我以前使用过Html.BeginCollectionItem()扩展,并且从视图到控制器的绑定没有问题。但是,在我当前的项目中,我遇到了问题。
模型
public class EditPathViewModel : TradeContractorEscalationPath
{
public Guid ContractorId { get; set; }
public List<ContactDisplayModel> Contacts { get; set; }
public List<TradeContractorEscalationPathContact> AssignedContacts { get; set; }
public int maxOrder { get; set; }
}
public partial class TradeContractorEscalationPath
{
public System.Guid Id { get; set; }
public System.Guid TradeContractorId { get; set; }
public string Description { get; set; }
public string Notes { get; set; }
public System.DateTime CreatedDate { get; set; }
public Nullable<System.DateTime> UpdatedDate { get; set; }
}
我正在尝试将AssignedContacts绑定回Controller,看起来像...
[HttpPost]
public async Task<ActionResult> EditPath([Bind(Include = "Id,TradeContractorId,Description,Notes,ContractorId,AssignedContacts,Contacts")] Models.EditPathViewModel model)
{
...
}
但是,它不断返回null。 BeginCollectionItem的配置如下:
TradeContractorEscalationPathContact的编辑器模板
@model Telecare.POCO.TradeContractorEscalationPathContact
@Html.EditorFor(model => model.Order)
@Html.EditorFor(model => model.TradeContractorEscalationPathId)
@Html.EditorFor(model => model.ContactId)
名为“分配的联系人”集合项的视图
...
<div class="col-lg-6">
<div class="ibox-title">
<h5>Contacts</h5>
</div>
<div class="ibox-content">
@Html.EditorFor(model => model.AssignedContacts)
</div>
</div>
...
调用POST时来自DOM的HTML示例
...
<input name="AssignedContacts.index" class="ui-sortable-handle" type="hidden" value="a5895b40-2de5-4ed5-a3ed-137c047963e8" autocomplete="off">
<input name="AssignedContacts[a5895b40-2de5-4ed5-a3ed-137c047963e8].Order" class="text-box single-line ui-sortable-handle" id="AssignedContacts_a5895b40-2de5-4ed5-a3ed-137c047963e8__Order" type="number" value="1" data-val-required="The Order field is required." data-val="true" data-val-number="The field Order must be a number.">
<input name="AssignedContacts[a5895b40-2de5-4ed5-a3ed-137c047963e8].TradeContractorEscalationPathId" class="text-box single-line ui-sortable-handle" id="AssignedContacts_a5895b40-2de5-4ed5-a3ed-137c047963e8__TradeContractorEscalationPathId" type="text" value="0807186a-3bad-e811-9c42-8cae4cf36195" data-val-required="The Trade Contractor Path field is required." data-val="true">
<input name="AssignedContacts[a5895b40-2de5-4ed5-a3ed-137c047963e8].ContactId" class="text-box single-line ui-sortable-handle" id="AssignedContacts_a5895b40-2de5-4ed5-a3ed-137c047963e8__ContactId" type="text" value="1e09c3ae-42ac-e811-9c42-8cae4cf36195" data-val-required="The Contact field is required." data-val="true">
<input name="AssignedContacts.index" class="ui-sortable-handle" type="hidden" value="974a0c38-642b-40f1-a5bd-dbc4b880eaa2" autocomplete="off">
<input name="AssignedContacts[974a0c38-642b-40f1-a5bd-dbc4b880eaa2].Order" class="text-box single-line ui-sortable-handle" id="AssignedContacts_974a0c38-642b-40f1-a5bd-dbc4b880eaa2__Order" type="number" value="2" data-val-required="The Order field is required." data-val="true" data-val-number="The field Order must be a number.">
<input name="AssignedContacts[974a0c38-642b-40f1-a5bd-dbc4b880eaa2].TradeContractorEscalationPathId" class="text-box single-line ui-sortable-handle" id="AssignedContacts_974a0c38-642b-40f1-a5bd-dbc4b880eaa2__TradeContractorEscalationPathId" type="text" value="0807186a-3bad-e811-9c42-8cae4cf36195" data-val-required="The Trade Contractor Path field is required." data-val="true">
<input name="AssignedContacts[974a0c38-642b-40f1-a5bd-dbc4b880eaa2].ContactId" class="text-box single-line ui-sortable-handle" id="AssignedContacts_974a0c38-642b-40f1-a5bd-dbc4b880eaa2__ContactId" type="text" value="f9d8afdd-42ac-e811-9c42-8cae4cf36195" data-val-required="The Contact field is required." data-val="true">
<input name="AssignedContacts.index" type="hidden" value="75a5e8e5-82e1-4f58-b185-ca6abefc5d9c" autocomplete="off">
<input name="AssignedContacts[75a5e8e5-82e1-4f58-b185-ca6abefc5d9c].Order" class="text-box single-line" id="AssignedContacts_75a5e8e5-82e1-4f58-b185-ca6abefc5d9c__Order" type="number" value="3" data-val-required="The Order field is required." data-val="true" data-val-number="The field Order must be a number.">
<input name="AssignedContacts[75a5e8e5-82e1-4f58-b185-ca6abefc5d9c].TradeContractorEscalationPathId" class="text-box single-line" id="AssignedContacts_75a5e8e5-82e1-4f58-b185-ca6abefc5d9c__TradeContractorEscalationPathId" type="text" value="0807186a-3bad-e811-9c42-8cae4cf36195" data-val-required="The Trade Contractor Path field is required." data-val="true">
<input name="AssignedContacts[75a5e8e5-82e1-4f58-b185-ca6abefc5d9c].ContactId" class="text-box single-line" id="AssignedContacts_75a5e8e5-82e1-4f58-b185-ca6abefc5d9c__ContactId" type="text" value="9ea0dcf8-42ac-e811-9c42-8cae4cf36195" data-val-required="The Contact field is required." data-val="true">
<input name="AssignedContacts.index" type="hidden" value="965b81be-f52c-4055-8b4b-bccf916f14a4" autocomplete="off">
<input name="AssignedContacts[965b81be-f52c-4055-8b4b-bccf916f14a4].Order" class="text-box single-line" id="AssignedContacts_965b81be-f52c-4055-8b4b-bccf916f14a4__Order" type="number" value="4" data-val-required="The Order field is required." data-val="true" data-val-number="The field Order must be a number.">
<input name="AssignedContacts[965b81be-f52c-4055-8b4b-bccf916f14a4].TradeContractorEscalationPathId" class="text-box single-line" id="AssignedContacts_965b81be-f52c-4055-8b4b-bccf916f14a4__TradeContractorEscalationPathId" type="text" value="0807186a-3bad-e811-9c42-8cae4cf36195" data-val-required="The Trade Contractor Path field is required." data-val="true">
<input name="AssignedContacts[965b81be-f52c-4055-8b4b-bccf916f14a4].ContactId" class="text-box single-line" id="AssignedContacts_965b81be-f52c-4055-8b4b-bccf916f14a4__ContactId" type="text" value="fc7a9669-43ac-e811-9c42-8cae4cf36195" data-val-required="The Contact field is required." data-val="true">
...
从我读过的所有线程中-HTML看起来应该正确绑定,所以我必须在其他地方缺少某些内容或做错了什么?有人有什么想法吗?
预先感谢
答案 0 :(得分:0)
好的-原来我是个白痴。上面的代码确实可以工作,但是没有绑定,因为它不在@ Html.BeginForm())部分中。叹气。