c#
  • asp.net-mvc
  • partial-views
  • 2017-03-24 57 views 1 likes 
    1

    我是MVC的新手。我需要通过从Request.Form读取值来在页面上创建隐藏字段。我在cshtml页面上使用了下面的代码。在返回部分视图之前修改视图

    @(new HtmlString("<input type='hidden' id='hdnLiveMatchItemID' value='" + HttpContext.Current.Request.Form["liveBlogMasterData[LiveMatchItemID]"] + "' />")) 
    @(new HtmlString("<input type='hidden' id='hdnMatchID' value='" + HttpContext.Current.Request.Form["liveBlogMasterData[MatchID]"] + "' />")) 
    @(new HtmlString("<input type='hidden' id='hdnMatchTagItemID' value='" + HttpContext.Current.Request.Form["liveBlogMasterData[MatchTagItemID]"] + "' />")) 
    @(new HtmlString("<input type='hidden' id='hdnLiveBlogEntryParentID' value='" + HttpContext.Current.Request.Form["liveBlogMasterData[LiveBlogEntryParentID]"] + "' />")) 
    

    但是这看起来很丑,我确信从服务器端一定有办法做到这一点。但我的服务器端代码返回部分如下。

    public PartialViewResult MyMethod() 
    { 
        return PartialView("somepath/somefile.cshtml"); 
    } 
    

    你能告诉我怎么能从服务器端做到这一点?返回前可能会修改PartialViewResult

    编辑:根据Vec **的建议。我使用下面的代码,但它给错误。

    @model IEnumerable<LiveBlogMasterData> 
          @foreach(var masterData in Model) 
          { 
           @Html.HiddenFor(m => masterData) 
          } 
    
    +0

    “CSHTML页码” *是*服务器端 - 不清楚你在问什么 – Lanorkin

    回答

    0

    你也可以做这样的事情在你的剃须刀模板:

    <input type='hidden' id='hdnLiveMatchItemID' value='@HttpContext.Current.Request.Form["liveBlogMasterData[LiveMatchItemID]"]' /> 
    <input type='hidden' id='hdnMatchID' value='@HttpContext.Current.Request.Form["liveBlogMasterData[MatchID]"]' /> 
    

    另一种选择是将值分配给ViewBag控制器:

    public PartialViewResult MyMethod() 
    { 
        ViewBag.LiveMatchId = HttpContext.Request.Form["liveBlogMasterData[LiveMatchItemID]"]; 
    } 
    

    and the View:

    <input type="hidden" id="hdnLiveMatchItemID" value="@ViewBag.LiveMatchId" /> 
    
    0

    你可以把你的字段列表,查看这样

    return PartialView("somepath/somefile.cshtml", list); 
    

    ,比尝试foreach循环使用,这样

    @model ....Models.ListOfHiddenItems 
    
        @foreach (var x in model) 
    
        { 
        @Html.HiddenFor(x => x.field) 
        } 
    
    +0

    获取错误。方法HiddenFor的类型参数不能从用法中推断出来。 – Akie

    相关问题