I have a “master” view (_MasterLayout.cshtml)
This has a Div, with an ID of “main”
<div id="main" class="xxx">
</div>
In views that inherit from _MasterLayout.cshtml, I need to be able to set the value of the class in this div (for example, set it to “product-page”)
How can I achieve this?
I’ve thought about using ViewBag, which would work –
ViewBag.MainDivCSS = "product-page";
However, this is a typo waiting to happen.
Are there any better ways of doing it?
The best would be if you wouldn’t have to set master page classes from child view page. For me, it’d be better to add one more div if this is all about nesting sections. Surround your whole child view with
and you would get
but anyways, if you want to, one option is to do that in single place in child view via js
I do not like making strongly view specific data carried by view models and managed by controller, so i prefer javascript approach in this case. And definitely, you’re right, you should not use
ViewBag.