几天来,我一直在为一个项目苦苦挣扎,在我当前的迭代中,我决定使用编辑器模板。
最初的问题源于通过 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>
}
这是编辑器模板。它的名称位于Views 文件夹
BankListAgentId
中的一个文件夹中EditorTemplates
@model Monet.Models.BankListAgentId
<tr id="item-@Model.AgentId">
<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="$('#item-@Model.AgentId').parent().remove();" style="float:right;">Delete</a></td>*@
</tr>
这是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; }
}
编辑
用户将需要能够动态更改列表。