Asp.net mvc 控制器can';无法接收模型数据
嗨,我想获取所有用户修改数据 我的问题是为什么控制器不能从我的项目中的视图接收模型数据 请解释导致此错误的原因以及解决方法 型号:Asp.net mvc 控制器can';无法接收模型数据,asp.net-mvc,Asp.net Mvc,嗨,我想获取所有用户修改数据 我的问题是为什么控制器不能从我的项目中的视图接收模型数据 请解释导致此错误的原因以及解决方法 型号: public class ShoppingCart { public List<ShoppingCartItemModel> items = new List<ShoppingCartItemModel>(); public IEnumerable<ShoppingCartItemModel&
public class ShoppingCart
{
public List<ShoppingCartItemModel> items = new List<ShoppingCartItemModel>();
public IEnumerable<ShoppingCartItemModel> Items
{
get { return items; }
}
}
public class ShoppingCartItemModel
{
public Product Product
{
get;
set;
}
public int Quantity { get; set; }
}
看法
@model ShoppingCart
@{
ViewBag.Title=”購物車內容";
}
指数
量
项目
价格
小计
@使用(Html.BeginForm(“EditFromCart”、“ShoppingCart”、FormMethod.Post))
{
foreach(Model.items中的var项)
{
@item.Product.ProductName
@项目.产品.价格.ToString(“c”)
@((item.Quantity*item.Product.Price).ToString(“c”))
@EditorFor(model=>item.Quantity,null,“UserInputQuantity”)
@隐藏(“ProductId”,item.Product.ProductId)
}
}
未正确设置文本和隐藏输入的名称:
@Html.EditorFor(model => item.Quantity, null, "UserInputQuantity")
@Html.Hidden("ProductId", item.Product.ProductID)
如果检查元素,可以看到名称是UserInputQuantity
和ProductId
,但它们应该是
项目[i]。数量
和项目[i]。产品。产品ID
您可以查看此链接:
您必须为复杂对象中要绑定的每个属性显式创建一个隐藏输入。IEnumerables和binding不能很好地直接开箱即用-看起来MVC对IList和数组有更好的基本支持,但您仍然必须枚举集合并为每个项创建隐藏输入。是否看看这个。所以,理想情况下,你的观点应该是:
@model ShoppingCart
@{
ViewBag.Title = "購物車內容";
}
<h2>Index</h2>
<table class="table">
<thead>
<tr>
<th>
Quantity
</th>
<th>
Item
</th>
<th class="text-right">
Price
</th>
<th class="text-right">
Subtotal
</th>
</tr>
</thead>
<tbody>
@using (Html.BeginForm("EditFromCart", "ShoppingCart", FormMethod.Post))
{
for (int i = 0; i < Model.items.Count(); ++i)
{
<tr>
<td class="text-center">
@Model.items[i].Product.ProductName
</td>
<td class="text-center">
@Model.items[i].Product.Price.ToString("c")
</td>
<td class="text-center">
@( (Model.items[i].Quantity * Model.items[i].Product.Price).ToString("c"))
</td>
<td class="text-left">
@Html.EditorFor(model => Model.items[i].Quantity)
@Html.HiddenFor(model => Model.items[i].Product.ProductID)
@Html.HiddenFor(model => Model.items[i].Product.ProductName)
@Html.HiddenFor(model => Model.items[i].Product.Price)
</td>
</tr>
}
<tr>
<td colspan="3">
<input class="btn btn-warning" type="submit" value="Edit">
</td>
</tr>
}
</tbody>
</table>
@model ShoppingCart
@{
ViewBag.Title=”購物車內容";
}
指数
量
项目
价格
小计
@使用(Html.BeginForm(“EditFromCart”、“ShoppingCart”、FormMethod.Post))
{
对于(int i=0;imodel.items[i].Quantity)
@Html.HiddenFor(model=>model.items[i].Product.ProductID)
@Html.HiddenFor(model=>model.items[i].Product.ProductName)
@Html.HiddenFor(model=>model.items[i].Product.Price)
}
}
如果您需要其他信息,请告诉我是否有商品列表(实体列表,在此情况下,在购物车中购物商品)无法使用简单的MVC/Razor提交回控制器。您需要使用JavaScript维护列表,并将其作为json等发布回。web上有很多关于ho开发MVC的ot文章cart@Emil当然可以!您只需要对它们进行适当的索引。模型绑定器也可以绑定集合和列表。
@Html.EditorFor(model => item.Quantity, null, "UserInputQuantity")
@Html.Hidden("ProductId", item.Product.ProductID)
@model ShoppingCart
@{
ViewBag.Title = "購物車內容";
}
<h2>Index</h2>
<table class="table">
<thead>
<tr>
<th>
Quantity
</th>
<th>
Item
</th>
<th class="text-right">
Price
</th>
<th class="text-right">
Subtotal
</th>
</tr>
</thead>
<tbody>
@using (Html.BeginForm("EditFromCart", "ShoppingCart", FormMethod.Post))
{
for (int i = 0; i < Model.items.Count(); ++i)
{
<tr>
<td class="text-center">
@Model.items[i].Product.ProductName
</td>
<td class="text-center">
@Model.items[i].Product.Price.ToString("c")
</td>
<td class="text-center">
@( (Model.items[i].Quantity * Model.items[i].Product.Price).ToString("c"))
</td>
<td class="text-left">
@Html.EditorFor(model => Model.items[i].Quantity)
@Html.HiddenFor(model => Model.items[i].Product.ProductID)
@Html.HiddenFor(model => Model.items[i].Product.ProductName)
@Html.HiddenFor(model => Model.items[i].Product.Price)
</td>
</tr>
}
<tr>
<td colspan="3">
<input class="btn btn-warning" type="submit" value="Edit">
</td>
</tr>
}
</tbody>
</table>