MvcShopping/Controllers/CartController.cs

(开头部分) 2KB

这里只显示每个文件的开头 60 行。登录后可以解锁完整代码。

using MvcShopping.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcShopping.Controllers
{
    public class CartController : BaseController
    {
        // 加入產品項目到購物車,如果沒有傳入 Amount 參數則預設購買數量為 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 existingCart = this.Carts.FirstOrDefault(p => p.Product.Id == ProductId);
            if (existingCart != null)
            {
                existingCart.Amount += 1;
            }
            else
            {
                this.Carts.Add(new Cart() { Product = product, Amount = Amount });
            }

            return new HttpStatusCodeResult(System.Net.HttpStatusCode.Created);
        }

        // 顯示目前的購物車項目
        public ActionResult Index()
        {
            return View(this.Carts);
        }

        // 移除購物車項目
        [HttpPost] // 因為知道要透過 AJAX 呼叫這個 Action,所以可以先標示 [HttpPost] 屬性
        public ActionResult Remove(int ProductId)
        {
            var existingCart = this.Carts.FirstOrDefault(p => p.Product.Id == ProductId);
            if (existingCart != null)
            {
                this.Carts.Remove(existingCart);
            }

            return new HttpStatusCodeResult(System.Net.HttpStatusCode.OK);
        }

        // 更新購物車中特定項目的購買數量
        [HttpPost] // 因為知道要透過 AJAX 呼叫這個 Action,所以可以先標示 [HttpPost] 屬性
        public ActionResult UpdateAmount(List<Cart> Carts)
        {
            foreach (var item in Carts)
            {
                var existingCart = this.Carts.FirstOrDefault(p => p.Product.Id == item.Product.Id);
后面还有 10 行代码,解锁后查看完整代码

24 小时内免费解锁 3 个项目,之后 1 积分/个。 规则说明

AI 解读

登录后可用,每次 10 积分,解读结果公开显示在下面。

还没有人解读过这个文件。