asp.net-mvc - MVC 4 _Layout.cshtml View 模型

标签 asp.net-mvc razor

我想在我的 MVC 站点的每个页面上显示所选项目的数量。我有一个 ViewModel 定义了我想要的属性

public class CartViewModel
{
    public List<CartItem> CartItems { get; set; }
    public decimal CartTotal { get; set; }
}

获取 Cart 的 Controller ,将其映射到 View 模型并将其传递给
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

最佳答案

尝试将以下内容添加到您的购物车 View 中:

@{Layout = null;}

关于asp.net-mvc - MVC 4 _Layout.cshtml View 模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15936768/

相关文章:

c# - MVC错误 "requires ienumerable instead of list"

javascript - ngif 无法与 ViewData 一起使用

asp.net-mvc - 使用 Autofac 解析 MVC5 应用程序中的 IOwinContext

ASP.NET MVC - 如何创建 RESTful Controller 方案?

c# - VS2017中的MVC 3错误: The name 'ViewData' does not exist in the current context

javascript - 如何更新 div 元素内 html 元素的 id 和名称?

c# - Kendo UI 引用在 Razor View 中不起作用

c# - Asp.NET MVC 和 Azure 存储

c# - Razor 组件的源生成器不工作

asp.net-mvc-3 - 不同命名空间中的 Asp.Net MVC 3 Custom WebViewPage 会破坏 IntelliSense