VM的回发返回空对象

本文关键字:对象 返回 VM | 更新日期: 2023-09-27 18:16:39

我填充了一个bicycleOrder列表并将其传递给视图。然后我遍历列表并在屏幕上显示所有订单。每个订单都有一个复选框,用于表示该订单是否已发货。我可以点击或取消选中复选框但是当我提交时。为什么它传递15个自行车订单记录都包含NULL值的post。在db表中有15条记录,但为什么每个记录中的每个值被张贴回NULL?

控制器

public ActionResult Index()
{
    BuyABicycle_Entities db1 = new BuyABicycle_Entities();
    List<Bicycle_Order> PopulatedOrderList = new List<Bicycle_Order>();
    List<BicycleOrder> All_Orders = (from c in db1.BicycleOrders
                                     where c.Id >= 1
                                     select c).ToList();
    foreach (BicycleOrder bik in All_Orders)
    {
        Bicycle_Order new_bik = new Bicycle_Order 
        {
            CustomerName = bik.CustomerName,
            CustomerAddress = bik.CustomerAddress,
            CustomerEmail = bik.CustomerEmail,
            CustomerPhoneNumber = bik.CustomerPhoneNumber,
            BicycleColour = bik.BicycleColour,
            BicycleModel = bik.BicycleModel,
            BicycleSize = bik.BicycleSize,
            Shipped = bik.Shipped
        };
        PopulatedOrderList.Add(new_bik);
    }
    SupplierVM model = new SupplierVM { allOrders = PopulatedOrderList };
    return View(model);
}      
[HttpPost]
public ActionResult Index(SupplierVM model)
{
    //write code:
    if (ModelState.IsValid)
    {          
        return RedirectToAction("Index");
    }
    return RedirectToAction("Index");
}
VM

public class SupplierVM
{
    public List<Bicycle_Order> allOrders { get; set; }
}
<<p> 视图/strong>
@model BicycleShop.ViewModels.SupplierVM
@{
    ViewBag.Title = "Supplier";
    var orders = (List<BicycleShop.Models.Bicycle_Order>)Model.allOrders;
}
@using (Html.BeginForm())
{
    <fieldset>
    <table class="items" summary="@("This is a table of all the orders for bicycles")">
        <colgroup>
            <col id="Col1" />
            <col id="Col2" />
            <col id="Col3" />
            <col id="Col4" />
            <col id="Col5" />
            <col id="Col6" />
            <col id="Col7" />
            <col id="Col8" />
        </colgroup>
        <thead>
            <tr>
                <th scope="col" width="15%">@("Name")</th>
                <th scope="col" width="15%">@("Address")</th>
                <th scope="col" width="15%">@("Phone")</th>
                <th scope="col" width="15%">@("Email")</th>
                <th scope="col" width="15%">@("Model")</th>
                <th scope="col" width="15%">@("Size")</th>
                <th scope="col" width="15%">@("Colour")</th>
                <th scope="col" width="15%">@("Shipped")</th>
            </tr>
        </thead>
         @for (int i = 0; i < Model.allOrders.Count; i++)
    {
    <text>
       <tr>
            <td width="15%">@Model.allOrders[i].CustomerName</td>   
            <td width="15%">@Model.allOrders[i].CustomerAddress</td>
            <td width="15%">@Model.allOrders[i].CustomerPhoneNumber</td>
            <td width="15%">@Model.allOrders[i].CustomerEmail</td>
            <td width="15%">@Model.allOrders[i].BicycleModel</td>
            <td width="15%">@Model.allOrders[i].BicycleSize</td>
            <td width="15%">@Model.allOrders[i].BicycleColour</td>
            <td width="15%">@Html.CheckBoxFor(x => @Model.allOrders[i].Shipped)</td>            
       </tr>
    </text>
    }   
    </table>
     <p>
         <input type="submit" value="Update" />
     </p>
    </fieldset>
}

有人能告诉我为什么在post back (SupplierVM post)返回"allOrders"列表中脚踏车订单的正确数量,但每个值都是NULL?

VM的回发返回空对象

您得到null是因为没有与表单集合关联的具有唯一名称属性的输入元素。Asp。. NET MVC模型绑定使用输入元素的name属性将表单数据绑定到模型集合。如果您查看过页面资源,就会了解这两种方法的控件名称是如何生成的。更改您的foreach如下

@for (int i = 0; i < Model.Count; i++)
{
   <text>
   <tr>
        <td width="15%">@Html.HiddenFor(item => Model[i].CustomerName)</td>   
        <td width="15%">@Html.HiddenFor(item => Model[i].CustomerAddress)</td>
        <td width="15%">@Html.HiddenFor(item => Model[i].CustomerPhoneNumber)</td>
        <td width="15%">@Html.HiddenFor(item => Model[i].CustomerEmail)</td>
        <td width="15%">@Html.HiddenFor(item => Model[i].BicycleModel)</td>
        <td width="15%">@Html.HiddenFor(item => Model[i].BicycleSize)</td>
        <td width="15%">@Html.HiddenFor(item => Model[i].BicycleColour)</td>
        <td width="15%">@Html.HiddenFor(item => Model[i].Shipped)</td>
        <td width="15%">@Html.CheckBoxFor(item => Model[i].Shipped)</td>            
   </tr>
   </text>
 }