MvcShopping/Controllers/CartController.cs
(开头部分) 2KB这里只显示每个文件的开头 60 行。登录后可以解锁完整代码。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcShopping.Models;
namespace MvcShopping.Controllers
{
public class CartController : Controller
{
MvcShoppingContext db = new MvcShoppingContext();
List<Cart> Carts
{
get
{
if (Session["Carts"] == null)
{
Session["Carts"] = new List<Cart>();
}
return (Session["Carts"] as List<Cart>);
}
set
{
Session["Carts"] = value;
}
}
//添加商品到购物车,如果没有传入Amout参数则默认购买1
[HttpPost]
//因为知道要通过Ajax调用此Action,所以标示为HttpPost
public ActionResult AddToCart(int productId,
int Amount = 1)
{
var product = db.Products.Find(productId);
//验证产品是否存在
if (product == null)
{
return HttpNotFound();
}
var existCart = this.Carts.FirstOrDefault(
p => p.product.Id == productId);
if (existCart != null)
{
existCart.Amount += 1;
}
else
{
this.Carts.Add(new Cart()
{
product = product,
Amount = Amount
});
}
return new HttpStatusCodeResult(
System.Net.HttpStatusCode.Created);
}
//移除购物车中的商品
[HttpPost]
public ActionResult Remove(int productId)
后面还有 36 行代码,解锁后查看完整代码
24 小时内免费解锁 3 个项目,之后 1 算力/个。 规则说明
AI 解读 AI
登录后可用,每次 10 算力,解读结果公开显示在下面。
还没有人解读过这个文件。