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:
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
As it is using the latest version