我有一个使用 View 模型的 View 。
在此 View 中,我在 @using (Html.BeginForm())
block 代码中使用部分 View ,如下所示
@foreach (var party in Model.Participants)
{
Html.RenderPartial("BlankEditorRow", party);
}
局部 View 有一些文本框字段,用户可以在这些字段中输入数据。 现在提交按钮没有放在局部 View 中,而是放在主视图中。
在我看来,当我点击提交按钮时,我在 View 模型中得到空值
[HttpPost]
public ActionResult ActionName(ViewModel model)
{
}
我不确定如何从部分 View 中获取帖子数据。任何人都可以帮助我了解如何从局部 View 发布数据吗?示例将是一个很大的帮助
编辑:部分 View 如下:
@model ASPNETMVCApplication.Models.Administration.Account.PartyModel
@using HtmlHelpers.BeginCollectionItem
<tr class="editorRow">
@using (Html.BeginCollectionItem("party"))
{
<td>@Html.TextBoxFor(m => m.FirstName, new { @style = "width: 100px;" })
</td>
<td>@Html.TextBoxFor(m => m.LastName, new { @style = "width: 100px;" })
</td>
<td>
@Html.DropDownListFor(model => model.PartyPersonalDetail.Gender, new SelectList(Model.Gender, "Id", "Name"), new { @style = "width: 100px;" })
</td>
<td>@Html.TextBoxFor(m => m.PartyPersonalDetail.MobilePhone, new { @style = "width: 100px;" })
</td>
<td>
@Html.DropDownListFor(model => model.PartyPersonalDetail.GothraId, new SelectList(Model.Gothras, "Id", "GothraName"), "--Don't Know--", new { @style = "width: 122px;" })
</td>
<td>
@Html.DropDownListFor(model => model.PartyPersonalDetail.NakshtraId, new SelectList(Model.Nakshtras, "Id", "NakshtraName"), "--Don't Know--", new { @style = "width: 122px;" })
</td>
<td>@Html.TextBoxFor(m => m.PartyPersonalDetail.EMail1, new { @style = "width: 135px;" })
</td>
<td>
<a href="#" class="deleteRow">Delete</a>
</td>
}
</tr>
最佳答案
为了在 MVC 中发布集合,它们必须被索引,因此您需要一个for
循环来代替foreach
。
试试这个:
@for(int i = 0; i < Model.Participants.Count; i++)
{
Html.RenderPartial("BlankEditorRow", Model.Participants[i]);
}
关于c# - 如何从 ASP.NET MVC3 中的局部 View 发布数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14480845/