C# 提交具有子集合的视图模型的表单
我想知道在MVC3中是否可以实现以下功能 我基本上有一个带有子集合的ViewModel,我想使用Ajax表单发布它: 模型 看法C# 提交具有子集合的视图模型的表单,c#,asp.net,asp.net-mvc,c#-4.0,C#,Asp.net,Asp.net Mvc,C# 4.0,我想知道在MVC3中是否可以实现以下功能 我基本上有一个带有子集合的ViewModel,我想使用Ajax表单发布它: 模型 看法 @model DropdownSave.Models.CustomerVM @{ 使用(Ajax.BeginForm(“客户”、“销售”、新的AjaxOptions{UpdateTargetId=“gridArea”、HttpMethod=“Post”})){ @foreach(模型客户中的var m) { @Html.TextBoxFor(a=>m.FirstNam
@model DropdownSave.Models.CustomerVM
@{
使用(Ajax.BeginForm(“客户”、“销售”、新的AjaxOptions{UpdateTargetId=“gridArea”、HttpMethod=“Post”})){
@foreach(模型客户中的var m)
{
@Html.TextBoxFor(a=>m.FirstName)
@Html.TextBoxFor(a=>m.LastName)
}
}
}
基本上,我可以将CustomerVm的实例发布到控制器,但子集合为null。我正在尝试的是可能的吗?我自己解决了:
看起来必须将视图更新为以下内容
<div>
@{
using(Ajax.BeginForm("Customers", "Sales", new AjaxOptions { UpdateTargetId = "gridArea",HttpMethod="Post" })){
<div id="gridArea">
<table>
@for (int i = 0; i < Model.Customers.Count; i++)
{
<tr>
<td>
@Html.LabelFor(m => m.Customers[i].FirstName)
</td>
<td>
@Html.LabelFor(m => m.Customers[i].LastName)
</td>
<td>
@Html.HiddenFor(m => m.Customers[i].CurrentSalesRep)
</td>
<td>
@Html.DropDownListFor(m => m.Customers[i].CurrentSalesRep,Model.Customers[i].SalesReps)
</td>
</tr>
}
</table>
<input type="submit" value="Save" />
</div>
}
}
</div>
@{
使用(Ajax.BeginForm(“客户”、“销售”、新的AjaxOptions{UpdateTargetId=“gridArea”、HttpMethod=“Post”})){
@对于(int i=0;im.Customers[i].FirstName)
@Html.LabelFor(m=>m.Customers[i].LastName)
@Html.HiddenFor(m=>m.Customers[i].CurrentSalesRep)
@Html.DropDownListFor(m=>m.Customers[i].CurrentSalesRep,Model.Customers[i].SalesReps)
}
}
}
我还添加了在下拉列表中捕获所选项目的功能
[HttpPost]
public ActionResult Customers(CustomerVM customer)
{
return PartialView(customer);
}
@model DropdownSave.Models.CustomerVM
<div>
@{
using(Ajax.BeginForm("Customers", "Sales", new AjaxOptions { UpdateTargetId = "gridArea",HttpMethod="Post" })){
<div id="gridArea">
<table>
@foreach (var m in Model.Customers)
{
<tr>
<td>
@Html.TextBoxFor(a => m.FirstName)
</td>
<td>
@Html.TextBoxFor(a => m.LastName)
</td>
</tr>
}
</table>
<input type="submit" value="Save" />
</div>
}
}
</div>
<div>
@{
using(Ajax.BeginForm("Customers", "Sales", new AjaxOptions { UpdateTargetId = "gridArea",HttpMethod="Post" })){
<div id="gridArea">
<table>
@for (int i = 0; i < Model.Customers.Count; i++)
{
<tr>
<td>
@Html.LabelFor(m => m.Customers[i].FirstName)
</td>
<td>
@Html.LabelFor(m => m.Customers[i].LastName)
</td>
<td>
@Html.HiddenFor(m => m.Customers[i].CurrentSalesRep)
</td>
<td>
@Html.DropDownListFor(m => m.Customers[i].CurrentSalesRep,Model.Customers[i].SalesReps)
</td>
</tr>
}
</table>
<input type="submit" value="Save" />
</div>
}
}
</div>