9.1.1 Creating a Custom Model Binder; Listing 9-3 Relying on the Model Binder in the CartController.cs File
This commit is contained in:
parent
767b09def1
commit
78b4ea3640
@ -18,49 +18,38 @@ namespace SportsStore.WebUI.Controllers
|
||||
repository = repo;
|
||||
}
|
||||
|
||||
public ViewResult Index(string returnUrl)
|
||||
public ViewResult Index(Cart cart, string returnUrl)
|
||||
{
|
||||
return View(new CartIndexViewModel
|
||||
{
|
||||
Cart = GetCart(),
|
||||
Cart = cart,
|
||||
ReturnUrl = returnUrl
|
||||
});
|
||||
}
|
||||
|
||||
// GET: Cart
|
||||
public RedirectToRouteResult AddToCart(int productId, string returnUrl)
|
||||
public RedirectToRouteResult AddToCart(Cart cart, int productId, string returnUrl)
|
||||
{
|
||||
Product product = repository.Products
|
||||
.FirstOrDefault(p => p.ProductID == productId);
|
||||
|
||||
if (product != null)
|
||||
{
|
||||
GetCart().AddItem(product, 1);
|
||||
cart.AddItem(product, 1);
|
||||
}
|
||||
return RedirectToAction("Index", new { returnUrl });
|
||||
}
|
||||
|
||||
public RedirectToRouteResult RemoveFromCart(int productId, string returnUrl)
|
||||
public RedirectToRouteResult RemoveFromCart(Cart cart, int productId, string returnUrl)
|
||||
{
|
||||
Product product = repository.Products
|
||||
.FirstOrDefault(p => p.ProductID == productId);
|
||||
|
||||
if (product != null)
|
||||
{
|
||||
GetCart().RemoveLine(product);
|
||||
cart.RemoveLine(product);
|
||||
}
|
||||
return RedirectToAction("Index", new { returnUrl });
|
||||
}
|
||||
|
||||
private Cart GetCart()
|
||||
{
|
||||
Cart cart = (Cart)Session["Cart"];
|
||||
if (cart == null)
|
||||
{
|
||||
cart = new Cart();
|
||||
Session["Cart"] = cart;
|
||||
}
|
||||
return cart;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user