精通asp.net mvc5创建购物车总结
应用商城不可或缺的购物车功能,总结如下
步骤1:定义购物车的实体,以及对加入购物车的一些方法,如增加删除清空等方法,在域模型中新增两个类,分别是 Cart 和CartLine.cs,代码如下:
public class Cart
{
private List<CartLine> lineCollection = new List<CartLine>();
public void AddItem(Product product,int quantity)
{
CartLine line = lineCollection
.Where(p => p.Product.ProductID == product.ProductID)
.FirstOrDefault();
if (line == null)
{
lineCollection.Add(new CartLine { Product = product, Quantity = quantity });
}
else
{
line.Quantity += quantity;
}
}
public void RemoveLine(Product product)
{
lineCollection.RemoveAll(l => l.Product.ProductID == product.ProductID);
}
public decimal ComputeTotalValue()
{
return lineCollection.Sum(e => e.Product.Price * e.Quantity);
}
public void Clear()
{
lineCollection.Clear();
}
public IEnumerable<CartLine> Lines
{
get { return lineCollection; }
}
public class CartLine
{
public Product Product { get; set; }
public int Quantity { get; set; }
}
步骤2:在视图中添加加入购物车按钮,加粗部分就是新增的购物车按钮.
@model ZlzStore.Domain.Entities.Product
<div class="well">
<h3>
<strong>@Model.Name </strong>
<span class="pull-right label label-primary">@Model.Price.ToString("c")</span>
</h3>
@using (Html.BeginForm("AddToCart", "Cart"))
{
<div class="pull-right">
@Html.HiddenFor(x=>x.ProductID)
@Html.Hidden("returnUrl",Request.Url.PathAndQuery)
<input type="submit" class="btn btn-success" value="加入购物车"/>
</div>
}
<span class="lead">@Model.Description</span>
</div>
步骤3:实现购物车控制器,新增控制器CartController
public class CartController : Controller
{
private IProductsRepository repository;
public CartController(IProductsRepository repo)
{
repository = repo;
}
public ViewResult Index(string returnUrl)
{
return View(new CartIndexViewModel
{
Cart = GetCart(),
ReturnUrl = returnUrl
});
}
public RedirectToRouteResult AddToCart(int productId,string returnUrl)
{
Product product = repository.Products
.FirstOrDefault(p => p.ProductID == productId);
if (product != null)
{
GetCart().AddItem(product, 1);
}
return RedirectToAction("Index", new { returnUrl });
}
public RedirectToRouteResult RemoveFromCart(int productId,string returnUrl)
{
Product product = repository.Products
.FirstOrDefault(p => p.ProductID == productId);
if (product != null)
{
GetCart().RemoveLine(product);
}
return RedirectToAction("Index", new { returnUrl });
}
private Cart GetCart()
{
Cart cart = (Cart)Session["Cart"];
if (cart == null)
{
cart = new Cart();
Session["Cart"] = cart;
}
return cart;
}
}
步骤4:显示购物车内容,
4.1首先新建一个购物车视图模型CartIndexViewModel
public class CartIndexViewModel
{
public Cart Cart { get; set; }
public string ReturnUrl { get; set; }
}
4.2然后创建Index视图,就是购物车数据显示的视图
@model ZlzStore.WebUI.Models.CartIndexViewModel
@{
ViewBag.Title = "ZlzStore:Your Cart";
}
<h2>Your cart</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>
@foreach (var line in Model.Cart.Lines)
{
<tr>
<td class="text-center">@line.Quantity</td>
<td class="text-center">@line.Product.Name</td>
<td class="text-center">@line.Product.Price.ToString("c")</td>
<td class="text-right">
@((line.Quantity*line.Product.Price).ToString("c"))
</td>
</tr>
}
</tbody>
<tfoot>
<tr>
<td colspan="3" class="text-right">Total:</td>
<td class="text-right">@Model.Cart.ComputeTotalValue().ToString("c")</td>
</tr>
</tfoot>
</table>
<div class="text-center">
<a class="btn btn-primary"href="@Model.ReturnUrl">继续购买</a>
</div>
以上就是所有内容,完成后的效果如下: