我有一个 ViewModel,其中包含我的模型类型的集合,如下所示:
public class OrderConfirm
{
public ICollection<QuoteLine> SalesLines { get; set; }
public string Currency { get; set; }
public int EnquiryID { get; set; }
}
我的 QuoteLine 模型如下所示:
public class QuoteLine
{
public int QuoteLineId { get; set; }
public int LostReasonId { get; set; }
public virtual LostReason LostReason { get; set; }
public string ItemName { get; set; }
}
在我看来,然后我在一个表单中遍历这些 QuoteLines 中的每一个,如下所示:
@using (Ajax.BeginForm("ConfirmLostOrder", new AjaxOptions()
{
InsertionMode = InsertionMode.Replace,
UpdateTargetId = "LostOrders",
OnBegin = "LostOrderConfirm"
}))
{
<table class="completed-enq-table">
<tr>
<th>
Item Number
</th>
<th>
Reason
</th>
</tr>
@foreach (var sales in Model.SalesLines)
{
<tr>
<td>@sales.ItemName
@Html.HiddenFor(model => sales.QuoteLineID)
</td>
<td>@Html.DropDownListFor(model => sales.LostReasonId, ((IEnumerable<myApp.Models.LostReason>)ViewBag.LostReasons).Select(option => new SelectListItem
{
Text = (option == null ? "None" : option.LostReason),
Value = option.LostReasonId.ToString(),
Selected = (Model != null) && (option.LostReasonId == sales.LostStatusId)
}))
</td>
</tr>
}
</table>
<input type="submit" style="float: right;" value="Submit Lost Order" />
}
然后我的HttpPost
动作看起来像这样:
[HttpPost]
public ActionResult ConfirmLostOrder(List<QuoteLine> models)
{
// process order
return PartialView("Sales/_ConfirmLostOrder");
}
问题是,models
为空。如果我使用 aFormCollection
我可以看到每个提交的值,但我想使用我的模型而不是 FormCollection 因为我想处理和编辑单独提交的每一行,因为它们可能有不同的原因