I have navbar elements in my _Layout.cshtml which depend on the controller being called. On a search page there will be no navigation but in order to keep the style of the site consistent the navbar itself will remain. I'm not sure what is the most accepted and idiomatic way of performing this work.
_Layout.cshtml
(etc)
<nav>
<div class="container">
<ul>
@if (TempData.ContainsKey(KeyChain.ItemKeyTempDataKey))**
{
var itemKey = TempData[KeyChain.ItemKeyTempDataKey] as ItemKey;
<li>@Html.ActionLink("Overview", "Index", "Overview", itemKey, new { })</li>
<li>@Html.ActionLink("Purchasing", "Index", "InvoiceMovementHistory", itemKey, new { })</li>
<li>@Html.ActionLink("Profit Trends", "Index", "SalesMovement", itemKey, new { })</li>
<li>@Html.ActionLink("Coupons", "Index", "Coupon", itemKey, new { })</li>
<li>@Html.ActionLink("Deals", "Index", "WebDeal", itemKey, new { })</li>
<li>@Html.ActionLink("Update Log", "Index", "UpdateLog", itemKey, new { })</li>
}
</ul>
</div>
</nav>
(etc)
ItemKey.cs
public class ItemKey
{
public string Upc { get; set; }
public string VendorItemCode { get; set; }
public int Vendor { get; set; }
}
UpdateLogViewModel.cs
public class UpdateLogViewModel
{
public IEnumerable<UpdateLogEntryViewModel> UpdateLogEntries { get; set; }
}
UpdateLogController.cs
public ActionResult Index(ItemKey itemKey)
{
TempData[KeyChain.ItemKeyTempDataKey] = itemKey;
//etc uses itemkey to get data in order to generate updateLogViewModel
return updateLogViewModel();
}
Things I thought of
Using TempData (as above) to display the navbar elements if the itemkey is populated. TempData, however, is kind of on its way out and feels hacky.
Add a rendersection to the navbar, put the navbar elements in a renderaction and populating them in the section on every view that uses it (which is essentially every view EXCEPT the search view). This just violates DRY on overdrive, but seems to me to be the idiomatic thing.
Derive a secondary sublayout that is an "itemlayout", which would be typed to itemkey and drops the tempdata check. At least provides compile-time checking as long as developers use the itemlayout for item subscreens. But, call me crazy, that's worse because now all of my derived view's viewmodels have to depend on the type from the itemlayout viewmodel. However, this has the advantage of making the dependency clear: if you're going to use this layout, you must derive from this viewmodel that contains an itemkey property. This seems like the most idiomatic way, but I hate the idea of a typed layout.
Move the navbar on to every view page. I will almost certainly not do this, but it should be mentioned that the possibility exists.
So, is there another way I could perform this action idiomatically in MVC, or is one of the options I've listed above the preferred method?