4

我有一个 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 因为我想处理和编辑单独提交的每一行,因为它们可能有不同的原因

4

1 回答 1

4

You can't use a foreach in this instance, it needs to be a for loop so that the name attributes of the fields contain the correct index so that default model binding knows it's binding to a list.

Firstly, I'm going to move your dropdown values out of the ViewBag (they should really be in there). That'll also take out some of that nasty logic in your view :)

So your model is now:

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

Try this instead of your 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>
}

Then change your post method to take your OrderConfirm model type:

[HttpPost]
public ActionResult ConfirmLostOrder(OrderConfirm model)
于 2012-05-29T11:13:42.667 回答