In my ASP.NET MVC application, in my _ViewStart.cshtml file, I have code like the following –
<div>SHOW THIS</div>
@{
Layout = "~/Views/Shared/_Layout.cshtml";
}
When the page is rendered, I expect “SHOW THIS” to be displayed above the content in the _Layout.cshtml page, but instead “SHOW THIS” is appearing below the the content in the _Layout.cshtml page.
Is this correct? If not, any idea why this would happen? Can you suggest how to make it show above the _Layout.cshtml content?
Thanks!
The layout is going to get loaded first and the content will be inserted into the layout’s render content section. If you want something to appear in the layout for this view you could try setting a ViewBag object in the controller and then check for that ViewBag object in the Layout.
Or you could use an optional section in the layout and then define it in the view:
In layout:
Then in your view: