MVC 4 _Layout.cshtml viewmodel

Sim*_*tin 8 asp.net-mvc razor

我想在我的MVC网站的每个页面上显示所选项目的计数.我有一个ViewModel,它定义了我想要的属性

public class CartViewModel
{
    public List<CartItem> CartItems { get; set; }
    public decimal CartTotal { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

获取Cart的控制器,将其映射到视图模型并传递它

public ActionResult GetCartSummary()
{
    var cart = Cart.Instance;
    var viewModel = AutoMapper.Mapper.Map<Cart, CartViewModel>(cart);
    return View(viewModel);
}
Run Code Online (Sandbox Code Playgroud)

并为此提出看法

@model TheWorkshop.Web.Models.Edit.ShoppingCartViewModel

<h2>Cart Summary</h2>
<span>@Model.CartTotal</span>
Run Code Online (Sandbox Code Playgroud)

最后在我的_Layout.cshtml文件中

@Html.Action("GetCartSummary", "Cart")
Run Code Online (Sandbox Code Playgroud)

但这给了我

System.StackOverflowException未处理

Jus*_*son 6

尝试将以下内容添加到购物车视图中:

@{Layout = null;}
Run Code Online (Sandbox Code Playgroud)