0

我一直在努力與一個項目了幾天,並在我目前的迭代,我已決定使用編輯器模板。ASP MVC3 - 編輯器模板返回空值到控制器回發

原始問題源於通過SQL中的外鍵鏈接到輔助表的主表。在ASP MVC,二次表所代表有以下字段:

[UIHint("BankListAgentId")] 
    public virtual ICollection<BankListAgentId> BankListAgentId { get; set; } 

因爲這是一個集合對象,「編輯」頁面上的GET適用於每一個具體的項目,但在POSTBACK所有集合項目突然無效。但是,所有其他字段都帶有正確的數據。我正在嘗試使用和編輯器模板,但收集項目仍在返回null

這裏是我使用視圖

@model Monet.Models.BankListMaster 
@using (Html.BeginForm()) 
{ 
    <fieldset> 
     <legend>Stat(s) Fixed</legend> 
     <table id="fixedRows"> 
      <tr> 
       <th>State Code</th> 
       <th>Agent ID</th> 
       <th></th> 
      </tr> 

      @foreach (var item in Model.BankListAgentId) 
      {      

       if (!String.IsNullOrWhiteSpace(item.AgentId) && item.FixedOrVariable.Equals("Fixed")) 
       { 
        @Html.EditorFor(model => item)      

       } 
      } 
     </table> 
     <br /> 
     @Html.ActionLink("Add another", "BlankFixedRow", null, null, new { id = "addFixed" }) 
    </fieldset> 
} 

代碼這裏是編輯模板。它的名字是BankListAgentId和在瀏覽文件夾 enter image description here

@model Monet.Models.BankListAgentId 

    <tr id="[email protected]"> 
     <td> 
      @Html.DropDownListFor(model => model.StateCode, 
       (SelectList)ViewBag.StateCodeList, Model.StateCode) 
     </td> 
     <td> 
      @Html.EditorFor(model => model.AgentId) 
      @Html.ValidationMessageFor(model => model.AgentId) 
     </td> 
     <td> 
      <a href="#" class="deleteRow">delete</a> 
     </td> 
     @*<td><a href="#" onclick="$('#[email protected]').parent().remove();" style="float:right;">Delete</a></td>*@ 
    </tr> 

這裏命名EditorTemplates文件夾中位於從BankListMaster模型

public partial class BankListMaster 
{ 
    public BankListMaster() 
    { 
     this.BankListStateCode = new HashSet<BankListStateCode>(); 
     this.BankListAgentId = new HashSet<BankListAgentId>(); 
     this.BankListAttachments = new HashSet<BankListAttachments>(); 
    } 

    public int ID { get; set; } 
    public string BankName { get; set; } 

    public virtual ICollection<BankListStateCode> BankListStateCode { get; set; } 
    public virtual ICollection<BankListAttachments> BankListAttachments { get; set; } 

    [UIHint("BankListAgentId")] 
    public virtual ICollection<BankListAgentId> BankListAgentId { get; set; } 
} 

最後這裏的代碼是BankListAgentId模型

public partial class BankListAgentId 
{ 
    public string AgentId { get; set; } 
    public int ID { get; set; } 
    public string FixedOrVariable { get; set; } 
    public string StateCode { get; set; } 

    public virtual BankListMaster BankListMaster { get; set; } 
} 

編輯

用戶需要能夠動態更改列表。

回答

0

我認爲你需要做這個迭代而不是foreach,以便字段名正確綁定。

@for (int i = 0; i < Model.BankListAgentId.Count; i++) 
      {      
       if (!String.IsNullOrWhiteSpace(Model.BankListAgentId[i].AgentId) && Model.BankListAgentId[i].FixedOrVariable.Equals("Fixed")) 
       { 
        @Html.EditorFor(model => Model.BankListAgentId[i]) 
       } 
      } 

你也應該發佈你的接收ActionResult。

P.S.但是,DaveA正確地指出該模型是ICollection,這會導致問題。它可以不是IEnumerable?

+0

ICollection的索引?這會破壞運行時。 – 2013-04-04 19:30:10

+0

好點,戴夫。謝謝。 – 2013-04-04 19:49:03

+0

我不認爲IEnumerable可以索引。將不得不使用IList。唯一的潛在問題是如果NealR需要使用foreach – 2013-04-04 19:50:18