asp.net-mvc - 从 MVC 区域的 _Layout.cshtml 添加数据到公共(public) _Layout.cshtml

标签 asp.net-mvc razor asp.net-mvc-areas

我有一个分为多个区域的 ASP.NET MVC 4 站点。每个区域都有一个Views/Shared/_Layout.cshtml引用公共(public)共享布局的 View 。在通用布局中,我有一个侧边栏,其中包含一个项目列表。我希望能够拥有所有 _Layout.cshtml 都可以访问的项目共享列表。 View 以聚合一组链接。

Area1/Views/Shared/_Layout.cshtml:

@{
   SidebarItems.Add("Area1 Item");
   Layout = "~/Views/Shared/_Layout.cshtml";
}

Views/Shared/_Layout.cshtml:

@{
   SidebarItems.Add("Common Item");
}
<ul>
@foreach (var item in SidebarItems)
{
   <li>@item</li>    @* List should contain two items: "Area1 Item", and "Common Item" *@
}
</ul>

我尝试了两种方法:

  1. 创建自定义 WebViewPage<T>继承自共同习俗的每个区域的类 WebViewPage<T>上课并制作SidebarItems收集公共(public)基类的一个属性。这不起作用,因为 Razor 似乎分配了一个新的 WebPageView在布局之间移动时。

  2. 创建一个带有静态集合的静态类,每个 _Layout调用添加项目。这成功地累积了列表项,但是,由于它是一个静态类,它的生命周期与应用程序域相关联,这意味着侧边栏从跨多个请求访问的每个区域累积项目,而不是每个请求列表。

我正在考虑使用 HttpRequest.Items属性,但这似乎太短暂了——项目列表不会因请求而改变,并且完全由显示的区域 View 决定。

另一种选择是将列表的呈现推送到 section 中。在每个区域的 _Layout 中呈现.这不太理想,因为我希望在呈现列表的代码中有一个点,但这是可行的。

建议?

最佳答案

您可以尝试使用 ViewBag。

我通过将名为 Items 的属性添加到 ViewBag 来完成快速测试。这将从每个区域(通过添加自己的项目)和通过添加公共(public)项目从主布局填充。然后它将用于呈现主布局中的项目列表。

Area1\Views\Shared_Layout.cshtml

@{
    ViewBag.Title = "_Layout";
    Layout = "~/Views/Shared/_Layout.cshtml";

    if (ViewBag.Items == null){ViewBag.Items = new List<String>();}
    ViewBag.Items.Add("Area1 item");
}

<h2>Area1 Layout</h2>

@RenderBody()

Views\Shared_Layout.cshtml(部分)

@{
    if (ViewBag.Items == null){ViewBag.Items = new List<String>();}
    ViewBag.Items.Add("Common Item");
}
<ul>
@foreach (var item in ViewBag.Items)
{
    <li>@item</li>    @* List should contain two items: "Area1 Item", and "Common Item" *@
}
</ul>

我不太喜欢该代码的外观,因为它重复了相当多的内容并传播了 ViewBag.Items 的用法。通过使用 Html 帮助程序将项目添加到列表并呈现列表,它可能会更干净。例如,您可以创建以下 2 个 Html 帮助程序:

public static class HtmlHelpers
{
    public static void AddCommonListItems(this HtmlHelper helper, params string[] values)
    {
        if(helper.ViewContext.ViewBag.Items == null) helper.ViewContext.ViewBag.Items=new List<String>();
        helper.ViewContext.ViewBag.Items.AddRange(values);
    }

    public static MvcHtmlString CommonList(this HtmlHelper helper)
    {
        if (helper.ViewContext.ViewBag.Items == null)
            return new MvcHtmlString(new TagBuilder("ul").ToString());

        var itemsList = new TagBuilder("ul");
        foreach (var item in helper.ViewContext.ViewBag.Items)
        {
            var listItem = new TagBuilder("li");
            listItem.SetInnerText(item);
            itemsList.InnerHtml += listItem.ToString();
        }
        return new MvcHtmlString(itemsList.ToString());

    }
}

然后您的 View 会看起来更干净,因为它们将只使用这些助手并避免重复代码:

Area1\Views\Shared_Layout.cshtml(使用新的 Html 助手)

@{
    ViewBag.Title = "_Layout";
    Layout = "~/Views/Shared/_Layout.cshtml";

    Html.AddCommonListItems("Area1 item", "Area1 item 2");
}

<h2>Area1 Layout</h2>

@RenderBody()

Views\Shared_Layout.cshtml(其中一部分,使用新的 Html 助手)

@{Html.AddCommonListItems("Common Item");}
@Html.CommonList()

关于asp.net-mvc - 从 MVC 区域的 _Layout.cshtml 添加数据到公共(public) _Layout.cshtml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16675656/

相关文章:

asp.net-mvc - Entity Framework 中的附加对象与分离对象

ASP.NET MVC - 对象引用未设置为对象的实例

asp.net-mvc - ASP.NET WebAPI 帖子正文中的空值?

c# - 下拉列表更好地作为 ViewBag 或模型 C#/.NET MVC4 的一部分

asp.net-mvc-3 - 在 Razor 中提交 parent 和 child

c# - 将多个单选按钮绑定(bind)到单个 bool 值

.NET - Razor MVC 应用程序之外 - 删除@inherits 和提供@model 的问题

c# - 将 RedirectToRouteResult 与 Area 一起使用失败

asp.net-mvc-2 - 是否有支持 ASP.NET MVC 2 区域功能的 MVC SiteMap 提供程序?

asp.net-mvc-2 - ASP.NET MVC2 区域在 IIS6 中不工作