Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 提交具有子集合的视图模型的表单_C#_Asp.net_Asp.net Mvc_C# 4.0 - Fatal编程技术网

C# 提交具有子集合的视图模型的表单

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

我想知道在MVC3中是否可以实现以下功能

我基本上有一个带有子集合的ViewModel,我想使用Ajax表单发布它:

模型

看法

@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>