Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 提交包含模型集合的表单_C#_.net_Asp.net Mvc_Asp.net Mvc 3_Razor - Fatal编程技术网

C# 提交包含模型集合的表单

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

我有一个ViewModel,其中包含我的模型类型的集合,如下所示:

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)