簡體   English   中英

在 ASP.Net 中制作購物車並添加折扣

[英]Making a Shopping Cart and adding discounts in ASP.Net

我正在嘗試學習 ASP.NET MVC,而我正在嘗試做的一個項目是用幾本書創建一個網站。 當您添加幾本同系列的書時,它應該提供折扣,但不適用於購物車中其他系列的任何書籍(例如 2 部漫威漫畫將給予 10% 的折扣,但對 DC 漫畫沒有任何折扣)。

但是我遇到的問題是首先創建一個購物車,我遵循以下教程:

https://learningprogramming.net/net/asp-net-core-mvc/build-shopping-cart-with-session-in-asp-net-core-mvc/

但它似乎過時了,我收到以下錯誤:

AmbiguousActionException:匹配多個操作。 以下操作匹配路由數據並滿足所有約束:

BookStore.Controllers.ProductController.Index (BookStore) 頁面:/Index

而且我不知道為什么,我是路由新手,所以我可以假設我錯過了一些東西,我將包含以下代碼:

namespace BookStore.Controllers
{
    [Route("product")]
    public class ProductController : Controller
    {
        [Route("")]
        [Route("index")]
        [Route("~/")]
        public IActionResult Index()
        {
            ProductModel productModel = new ProductModel();
            ViewBag.products = productModel.findAll();
            return View();
        }
    }
}

namespace BookStore.Controllers
{
    [Route("cart")]
    public class CartController : Controller
    {
        [Route("index")]
        public IActionResult Index()
        {
            var cart = SessionHelper.GetObjectFromJson<List<Item>>(HttpContext.Session, "cart");
            ViewBag.cart = cart;
            ViewBag.total = cart.Sum(item => item.Product.Price * item.Quantity);
            return View();
        }

        [Route("buy/{id}")]
        public IActionResult Buy(string id)
        {
            ProductModel productModel = new ProductModel();
            if (SessionHelper.GetObjectFromJson<List<Item>>(HttpContext.Session, "cart") == null)
            {
                List<Item> cart = new List<Item>();
                cart.Add(new Item { Product = productModel.find(id), Quantity = 1 });
                SessionHelper.SetObjectAsJson(HttpContext.Session, "cart", cart);
            }
            else
            {
                List<Item> cart = SessionHelper.GetObjectFromJson<List<Item>>(HttpContext.Session, "cart");
                int index = isExist(id);
                if (index != -1)
                {
                    cart[index].Quantity++;
                }
                else
                {
                    cart.Add(new Item { Product = productModel.find(id), Quantity = 1 });
                }
                SessionHelper.SetObjectAsJson(HttpContext.Session, "cart", cart);
            }
            return RedirectToAction("Index");
        }
    }
}

我也一直在看這個

https://docs.microsoft.com/en-us/aspnet/web-forms/overview/getting-started/getting-started-with-aspnet-45-web-forms/create_the_data_access_layer

因為它使用的是最新版本

您可以將Name屬性添加到Controller方法Index的 HTTP 動詞屬性中。 在您的情況下,您可以這樣做:

//ProductController Index method
[Route("")]
[Route("index")]
[Route("~/")]
[HttpGet(Name = "Get my products")]
public IActionResult Index()

//CartController Index method
[Route("index")]
[HttpGet(Name = "Get my cart data")]
public IActionResult Index()

你可以在這里閱讀更多為什么你不能在不同的控制器上有兩個同名的路徑

您還需要正確設置routes

app.UseMvc(routes =>  
{  
    routes.MapRoute(
      name: "ByProduct",
      template: "{controller}/{action}/{id?}",
      defaults: new { controller = "Product", action = "Index"});  

  routes.MapRoute(  
      name: "default",  
      template: "{controller=Home}/{action=Index}/{id?}");  
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM