I want to display a count of selected items on every page in my MVC site. I have a ViewModel that defines the properties I want there
public class CartViewModel
{
public List<CartItem> CartItems { get; set; }
public decimal CartTotal { get; set; }
}
a controller that gets the Cart, maps it to the view model and passes that on
public ActionResult GetCartSummary()
{
var cart = Cart.Instance;
var viewModel = AutoMapper.Mapper.Map<Cart, CartViewModel>(cart);
return View(viewModel);
}
and a view for that
@model TheWorkshop.Web.Models.Edit.ShoppingCartViewModel
<h2>Cart Summary</h2>
<span>@Model.CartTotal</span>
and finally in my _Layout.cshtml
file
@Html.Action("GetCartSummary", "Cart")
But this gives me
System.StackOverflowException was unhandled
Try adding the following to your cart view:
@{Layout = null;}
Try returning a PartialView instead of View:
public ActionResult GetCartSummary()
{
var cart = Cart.Instance;
var viewModel = AutoMapper.Mapper.Map<Cart, CartViewModel>(cart);
return PartialView(viewModel);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With