asp.net-mvc – MVC 4 _Layout.cshtml viewmodel

我想在我的MVC站点的每个页面上显示所选项目的计数.我有一个ViewModel,它定义了我想要的属性
public class CartViewModel
{
    public List<CartItem> CartItems { get; set; }
    public decimal CartTotal { get; set; }
}

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

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

并对此有所了解

@model TheWorkshop.Web.Models.Edit.ShoppingCartViewModel

<h2>Cart Summary</h2>
<span>@Model.CartTotal</span>

最后在我的_Layout.cshtml文件中

@Html.Action("GetCartSummary","Cart")

但这给了我

System.StackOverflowException was unhandled

解决方法

尝试将以下内容添加到购物车视图中:
@{Layout = null;}

相关文章

判断URL文件是不是在于在。private static bool UrlIsExist(...
由于在.net中,Request时出现有HTML或Javascript等字符串时,...
public static bool ProcessIdCard(this string idCard, out...
protected void Page_Load(object sender, EventArgs e){ Sc...