C# 提交包含模型集合的表单
我有一个ViewModel,其中包含我的模型类型的集合,如下所示:C# 提交包含模型集合的表单,c#,.net,asp.net-mvc,asp.net-mvc-3,razor,C#,.net,Asp.net Mvc,Asp.net Mvc 3,Razor,我有一个ViewModel,其中包含我的模型类型的集合,如下所示: public class OrderConfirm { public ICollection<QuoteLine> SalesLines { get; set; } public string Currency { get; set; } public int EnquiryID { get; set; } } @using (Ajax.BeginForm("ConfirmLostOrder
public class OrderConfirm
{
public ICollection<QuoteLine> SalesLines { get; set; }
public string Currency { get; set; }
public int EnquiryID { get; set; }
}
@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" />
}
在我看来,然后我在一个表单中迭代这些引号,如下所示:
public class OrderConfirm
{
public ICollection<QuoteLine> SalesLines { get; set; }
public string Currency { get; set; }
public int EnquiryID { get; set; }
}
@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" />
}
问题是,
模型
为空。如果我使用FormCollection
我可以看到提交的每个值,但我希望使用我的模型而不是FormCollection,因为我希望单独处理和编辑提交的每一行,因为它们可能有不同的原因在本例中,您不能使用foreach
,它需要是for
循环,以便字段的name
属性包含正确的索引,以便默认模型绑定知道它绑定到列表
首先,我要将您的下拉值从视图包中移出(它们应该真的在那里)。这也将消除您认为的一些令人讨厌的逻辑:)
因此,您的模型现在是:
public class OrderConfirm
{
public List<QuoteLine> SalesLines { get; set; }
public string Currency { get; set; }
public int EnquiryID { get; set; }
public SelectList LostReasons { get; set; }
}
然后更改post方法以获取您的订单确认
型号:
[HttpPost]
public ActionResult ConfirmLostOrder(OrderConfirm model)
该视图中的模型类型是什么?是订单确认吗?是的,对不起,我应该这么说!您能否尝试“public ActionResult ConfirmLostOrder(OrderConfirm model)”,然后从model.SalesLine访问每个QuoteLine?我以前做过,但值都为null、Currency null、InquiryId 0、SalesLines nullI get:
无法将带[]的索引应用于“System.Collections.Generic.ICollection”类型的表达式。
@BiffBaffBoff Ah,您不能将其更改为列表吗
?太好了!这似乎有效!非常感谢,我也将这个Viewbag移到了我的ViewModel中@没问题!很高兴我能帮忙:)。是的,那是更好的练习
[HttpPost]
public ActionResult ConfirmLostOrder(OrderConfirm model)