You can try using the ViewBag for it.
I have done a quick test by adding a property named Items
to the ViewBag. This will be populated from every area (by adding its own items) and from the main layout by adding the common items. It will then be used to render the list of items from the main layout.
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 (part of it)
@{
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>
I don't like much how that code looks, as it is repeating quite a bit and spreading the usage of ViewBag.Items. It could be cleaner by using Html helpers for adding the items to the list and rendering the list. For example, you could create the following 2 Html helpers:
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());
}
}
And then your views would look cleaner, as they will just use those helpers and avoid repeating the code:
Area1\Views\Shared_Layout.cshtml (using new Html helpers)
@{
ViewBag.Title = "_Layout";
Layout = "~/Views/Shared/_Layout.cshtml";
Html.AddCommonListItems("Area1 item", "Area1 item 2");
}
<h2>Area1 Layout</h2>
@RenderBody()
Views\Shared_Layout.cshtml (part of it, using new Html helpers)
@{Html.AddCommonListItems("Common Item");}
@Html.CommonList()