0

几天来,我一直在为一个项目苦苦挣扎,在我当前的迭代中,我决定使用编辑器模板。

最初的问题源于通过 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; }
}

编辑

用户将需要能够动态更改列表。

4

1 回答 1

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。

PS 但正如 DaveA 正确指出的那样,模型是 ICollection,这会导致问题。它不能是 IEnumerable 吗?

于 2013-04-04T19:25:51.990 回答