Iv填充了bikeOrder列表并将其传递给视图。然后我浏览列表并在屏幕上显示所有订单。每个订单都有一个复选框,用于确定订单是否已发货。我可以单击或取消选中该复选框,但是当我提交时。为什么它会将包含NULL值的15个bicycleOrder记录传递给帖子。 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; }
}
查看
@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>
}
有人可以告诉我为什么在帖子后面(SupplierVM帖子)会在&#39; allOrders&#39;中返回正确数量的BicycleOrders。列表,但每个值都是NULL?
答案 0 :(得分:2)
您得到null
,因为没有与您的表单集合关联的唯一名称属性的输入元素。 Asp.NET MVC模型绑定使用您的输入元素&#39; name属性,用于将表单数据绑定到模型集合。如果您查看了您的pagesource,您将了解如何为这两种方法生成控件名称。更改您的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>
}