asp.net的MVC模型绑定失败&LT;列表&gt;在编辑模板项目 [英] asp.net mvc model binding fails for <List> items in editor template
问题描述
我搜索的SO,仍然不知道我做错了这里的许多职位。我有一个模式
的订单,其中包括一个&LT;列表&gt;
OrderItem的的
I've searched many posts on SO and still not sure what I did wrong here. I have a model
for "Order" which includes a <list>
of "OrderItem"
public class Order
{
public int OrderId { get; set; }
public int CustId { get; set; }
public DateTime OrderDate { get; set; }
public int OrderType { get; set; }
...
...
public List<OrderItem> OrderItems = new List<OrderItem>();
}
public class OrderItem
{
public string ProductCode { get; set; }
public decimal RetailPrice { get; set; }
public string ProductQuantity { get; set; }
}
在我看来,这是强类型的订单模式,我使用的是编辑模板显示订单项
In my view, which is strongly typed to the Order model, I am using an Editor Template to display the order items
@model FTG.Models.Order
@Html.EditorFor(x => x.OrderItems)
和编辑模板,似乎为模型绑定发生指定一个合适的名称:
and the editor template seems to assign a proper name for the model binding to occur:
input type="number" id="OrderItems_0__ProductQuantity" name="OrderItems[0].ProductQuantity"...
input type="number" id="OrderItems_1__ProductQuantity" name="OrderItems[1].ProductQuantity"...
但我的模型回来控制器与计数= 0
的名单。该模型的其余看起来不错,我只是不能从的OrderItems列表中获取值。
But my model comes back to the controller with count=0
for the list. The rest of the model looks fine, I just can't get the values from the list of orderitems.
有谁知道我做错了,或者什么,我缺少什么?
Does anyone know what I am doing wrong, or what I am missing?
推荐答案
我觉得你的模型类是缺少对孩子列表中的属性。尝试添加属性的列表的OrderItems
I think your model class is missing a property for the child list. Try adding a property for the OrderItems list.
这篇关于asp.net的MVC模型绑定失败&LT;列表&gt;在编辑模板项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!