簡體   English   中英

集合未在提交表單時更新

[英]Collection not updating on form submission

我有一個看起來像這樣的復雜模型:

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在表單提交中提交。

如果將對象添加到構造函數中的列表中,則ModelThree最初ModelThree綁定,如果我將對象添加到構造函數中的列表中,它們將按預期呈現,但在提交表單時永遠不會更新。 是添加項目(在此示例中我排除了添加邏輯)還是通過生成的下拉列表更新現有項目。

我需要怎么做才能正確提交? 據我了解,生成的HTML帶有索引似乎是正常的,據我了解,它應該弄清楚如何從那里進行綁定。

注意:我也嘗試了通用dropdownlist而不是kendo組合列表,結果相同。

查看是否可行:

<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 ,因此所有父模型上下文都將丟失,並且助手不會在輸入元素上呈現正確的名稱屬性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM