集合未在表单提交时更新

时间:2014-02-08 23:10:56

标签: c# ajax asp.net-mvc asp.net-mvc-4

我有一个看起来像这样的复杂模型:

public class ModelOne()
{
    //other stuff
    public ModelTwo modelTwo {get;set;}
}

public class ModelTwo()
{
    //other stuff
    public List<ModelThree> modelThrees {get;set;}
}

public class ModelThree()
{
    public int Id {get;set;}
    public string Type {get;set;}
}

主视图

@model ModelOne

@using (Html.BeginForm("blah", "blah", FormMethod.Post, new { id = "blah" }))
{ 

    //other form fields

    <div id="partial">
        @{Html.RenderPartial("partialView", ModelOne.modelTwo.modelThrees);
    </div>

     <input id="submitForm" type="submit" value="Submit" />    

}

ModelThree的部分视图

@model IList<ModelThree>

<table>
    //header
    @{for (int i = 0; i < Model.Count(); i++)
    {
        <tr>
            @Html.HiddenFor(m => m[i].Id)
            <td>
                @(Html.Kendo().ComboBoxFor(m => m[i].Type)
                    .Filter("contains")
                    .Placeholder("Select type...")
                    .DataTextField("Text")
                    .DataValueField("Value")
                    .BindTo(new List<SelectListItem>() {
                        new SelectListItem() {
                            Text = "Some type", Value = "SomeType"
                        },
                        new SelectListItem() {
                            Text = "Other type", Value = "OtherType"
                        }
                    })
                    .HtmlAttributes(new { style = "width: 250px;" })
                )
            </td>
        </tr>
    }}
</table>

除了ModelThree集合之外的所有内容都会在表单提交上提交。

ModelThree最初应该绑定,如果我在构造函数中将对象添加到我的列表中,它们按预期呈现,但在提交表单时永远不会更新。我是否添加了项目(我从此示例中排除了添加逻辑)或通过生成的下拉列表更新现有项目。

如果正确提交,我需要做什么?生成的HTML在索引时看起来很正常,据我所知它应该弄清楚如何从那里绑定它。

注意:我也尝试过通用下拉列表而不是kendo组合列表,结果相同。

1 个答案:

答案 0 :(得分:1)

看看是否有效:

<div id="partial">
    @{Html.RenderPartial("partialView");
</div>

...

@model ModelOne

<table>
    //header
    @foreach (var model3 in Model.modelTwo.modelThrees)
    {
        <tr>
            @Html.HiddenFor(m => model3.Id)
            <td>
                @(Html.Kendo().ComboBoxFor(m => model3.Type)
                    .Filter("contains")
                    .Placeholder("Select type...")
                    .DataTextField("Text")
                    .DataValueField("Value")
                    .BindTo(new List<SelectListItem>() {
                        new SelectListItem() {
                            Text = "Some type", Value = "SomeType"
                        },
                        new SelectListItem() {
                            Text = "Other type", Value = "OtherType"
                        }
                    })
                    .HtmlAttributes(new { style = "width: 250px;" })
                )
            </td>
        </tr>
    }
</table>

这对我来说就像一个模型绑定问题。由于您将第二个参数传递给RenderPartial,因此所有父模型上下文都将丢失,并且帮助程序不会在输入元素上呈现正确的名称属性。

相关问题