2014-02-08 53 views
0

我有一個複雜的模型,看起來像這樣:集合不更新表單提交

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在編制索引時看起來很正常,因爲我知道它應該弄清楚如何從那裏綁定它。

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

回答

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

+0

這樣做了,謝謝! FYI for循環仍然需要,但允許唯一的名稱。 – aw04