MVC3如何实现多个“添加到购物车”购物车中的功能?
我按照音乐商店 mvc 示例创建了自己的简单购物车。问题是,我的商店一次只允许添加一件商品。但现在我需要一种方法来一次将多个商品添加到购物车并指定数量。
我正在考虑这样做: 模型绑定到列表,但我的商品模型没有数量属性来保存所需的购物车数量。
public int ItemID { get; set; }
public string Barcode { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public decimal Price { get; set; }
和我的视图模型:
public class ItemViewModel
{
public int ItemID { get; set; }
public string Barcode { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public decimal Price { get; set; }
public int Quantity { get; set; }
}
public class ItemToCartViewModel
{
public List<ItemViewModel> itemToCart { get; set; }
}
似乎没有在我的后操作中正确发布(空或没有传递值):
[HttpPost]
public ActionResult addtocart(ICollection<ItemViewModel> items)
{ ... }
有什么想法吗?
更新:
@model IList<MyTGP.ViewModels.ItemViewModel>
@{
ViewBag.Title = "Search for Items";
int count = 0;
}
<h2>
Search for Items</h2>
@Html.Partial("_SearchItemsPartial")
@using (Html.BeginForm("addtocart","Cart"))
{
if (String.IsNullOrEmpty(ViewBag.NoRecord)) {
<table>
<tr>
<th>
Barcode
</th>
<th>
Name
</th>
<th>
Description
</th>
<th>
Price
</th>
<th>
Quantity
</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Barcode)
</td>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Description)
</td>
<td>
@Html.DisplayFor(modelItem => item.Price)
</td>
<td>
@Html.EditorFor(modelItem => item.Quantity)
</td>
</tr>
count++;
}
</table>
} else { @ViewBag.NoRecord }
<p>
<input type="submit" value="add items to cart" />
</p>
}
I created my own simple shopping cart following the Music store mvc example. Problem is, my shop only allows one item addition at a time. But now I need a way to add multiple items at once to a cart and specify the quantity.
I was thinking about doing this: Model Binding to a List, but my Item model does not have a quantity property to hold the needed cart quantity.
public int ItemID { get; set; }
public string Barcode { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public decimal Price { get; set; }
and my viewmodel:
public class ItemViewModel
{
public int ItemID { get; set; }
public string Barcode { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public decimal Price { get; set; }
public int Quantity { get; set; }
}
public class ItemToCartViewModel
{
public List<ItemViewModel> itemToCart { get; set; }
}
doesn't seem to post properly in my post action (null or no values passed):
[HttpPost]
public ActionResult addtocart(ICollection<ItemViewModel> items)
{ ... }
Any ideas?
UPDATE:
@model IList<MyTGP.ViewModels.ItemViewModel>
@{
ViewBag.Title = "Search for Items";
int count = 0;
}
<h2>
Search for Items</h2>
@Html.Partial("_SearchItemsPartial")
@using (Html.BeginForm("addtocart","Cart"))
{
if (String.IsNullOrEmpty(ViewBag.NoRecord)) {
<table>
<tr>
<th>
Barcode
</th>
<th>
Name
</th>
<th>
Description
</th>
<th>
Price
</th>
<th>
Quantity
</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Barcode)
</td>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Description)
</td>
<td>
@Html.DisplayFor(modelItem => item.Price)
</td>
<td>
@Html.EditorFor(modelItem => item.Quantity)
</td>
</tr>
count++;
}
</table>
} else { @ViewBag.NoRecord }
<p>
<input type="submit" value="add items to cart" />
</p>
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我还不能发表评论,所以我在这里发帖。你能给我们看看风景吗? html很重要。
更新:
尝试这样(在 foreach 循环中)
然后在哪里显示数量
@Html.TextBox("["+count+"].Quantity",item.Quantity)
I can't comment yet so I'm posting here. Can you show us the view? The html is important.
Update:
Try like this (in the foreach loop)
Then where you display the quantity
@Html.TextBox("["+count+"].Quantity",item.Quantity)