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
和在瀏覽文件夾
@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; }
}
編輯
用戶需要能夠動態更改列表。
ICollection的索引?這會破壞運行時。 – 2013-04-04 19:30:10
好點,戴夫。謝謝。 – 2013-04-04 19:49:03
我不認爲IEnumerable可以索引。將不得不使用IList。唯一的潛在問題是如果NealR需要使用foreach – 2013-04-04 19:50:18