简体   繁体   中英

Making a Shopping Cart and adding discounts in ASP.Net

I am trying to learn ASP.NET MVC, and a project I am trying to do is to create a website with a few books. When you add a few books of the same series, it should give a discount but does not apply to any book of another series in a Shopping cart (for example 2 Marvel comics would give 10% off those but not any discount on a DC comic).

However the issue I am having is creating a shopping cart in the first place, I was following the following tutorial on this:

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

But it seems out of date and I get an error of:

AmbiguousActionException: Multiple actions matched. The following actions matched route data and had all constraints satisfied:

BookStore.Controllers.ProductController.Index (BookStore) Page: /Index

And I am not sure why, I'm new to routing so I can assume I've missed something, I'll include the code below:

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");
        }
    }
}

And I have been taking a look at this one as well

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

As it is using the latest version

You can add the Name property to your HTTP verb attribute for your Controller method- Index . In your case, you can do this:

//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()

You can read more why you can't have two paths with same name on different controllers here

You also need to setup your routes correctly:

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?}");  
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM