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