【问题标题】:Collection not updating on form submission表单提交时集合未更新
【发布时间】:2014-02-09 07:28:31
【问题描述】:

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

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 通过索引看起来很正常,据我了解,它应该弄清楚如何从那里绑定它。

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

【问题讨论】:

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


    【解决方案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,因此您的所有父模型上下文都将丢失,并且助手不会在您的输入元素上呈现正确的名称属性。

    【讨论】:

    • 成功了,谢谢!仅供参考尽管允许唯一名称,但仍然需要 for 循环。
    猜你喜欢
    • 2017-05-21
    • 1970-01-01
    • 2021-09-05
    • 1970-01-01
    • 2013-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-27
    相关资源
    最近更新 更多