【问题标题】:Submit a form with a collection of a Model提交包含模型集合的表单
【发布时间】:2012-05-29 10:35:27
【问题描述】:

我有一个 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 为空。如果我使用FormCollection,我可以看到每个提交的值,但我想使用我的模型而不是 FormCollection,因为我想处理和编辑单独提交的每一行,因为它们可能有不同的原因

【问题讨论】:

  • 您的模型在该视图中的类型是什么?是 OrderConfirm 吗?
  • 是的,对不起,我应该这么说!
  • 你可以试试“public ActionResult ConfirmLostOrder(OrderConfirm model)”然后从model.SalesLine访问每个QuoteLine吗?
  • 我之前做过,但值都是 null,Currency null,EnquiryId 0,SalesLines null

标签: c# .net asp.net-mvc asp.net-mvc-3 razor


【解决方案1】:

在这种情况下,您不能使用 foreach,它必须是 for 循环,以便字段的 name 属性包含正确的索引,以便默认模型绑定知道它绑定到列表.

首先,我要将您的下拉值移出ViewBag(它们确实应该在其中)。这也将消除您认为的一些讨厌的逻辑:)

所以你的模型现在是:

public class OrderConfirm
{
    public List<QuoteLine> SalesLines { get; set; }
    public string Currency { get; set; }
    public int EnquiryID { get; set; }
    public SelectList LostReasons { get; set; }
}

试试这个而不是你的foreach

@for (var i = 0; i < Model.SalesLines.Count; i++)
{
    <tr>
        <td>
            @Model.SalesLines[i].ItemName
            @Html.HiddenFor(m => m.SalesLines[i].QuoteLineId)
            @Html.HiddenFor(m => m.SalesLines[i].ItemName) //assuming you want this
        </td>
        <td>
            @Html.DropDownListFor(m => m.SalesLines[i].LostReasonId, Model.LostReasons)
       </td>
   </tr>
}

然后更改您的 post 方法以采用您的 OrderConfirm 模型类型:

[HttpPost]
public ActionResult ConfirmLostOrder(OrderConfirm model)

【讨论】:

  • 我得到:Cannot apply indexing with [] to an expression of type 'System.Collections.Generic.ICollection&lt;myApp.Models.QuoteLine&gt;'
  • @BiffBaffBoff 啊,不能改成List吗?
  • 太棒了!这似乎有效!非常感谢,我也将那个 Viewbag 移到了我的 ViewModel 中!
  • @BiffBaffBoff 没问题!很高兴我能帮上忙 :)。是的,这样做是更好的做法! :)
猜你喜欢
  • 1970-01-01
  • 2011-08-22
  • 1970-01-01
  • 2017-03-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-21
相关资源
最近更新 更多