我是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)
}
“CSHTML页码” *是*服务器端 - 不清楚你在问什么 – Lanorkin