[英]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.我正在尝试学习 ASP.NET MVC,而我正在尝试做的一个项目是用几本书创建一个网站。 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).
当您添加几本同系列的书时,它应该提供折扣,但不适用于购物车中其他系列的任何书籍(例如 2 部漫威漫画将给予 10% 的折扣,但对 DC 漫画没有任何折扣)。
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/ 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.
AmbiguousActionException:匹配多个操作。 The following actions matched route data and had all constraints satisfied:
以下操作匹配路由数据并满足所有约束:
BookStore.Controllers.ProductController.Index (BookStore) Page: /Index
BookStore.Controllers.ProductController.Index (BookStore) 页面:/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 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
.您可以将
Name
属性添加到Controller
方法Index
的 HTTP 动词属性中。 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:您还需要正确设置
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.