Can I conditionally render partial views in _Layout.cshtml?
asp.net-mvc, partial-views
Solution
In your layout, add this `section`
@RenderSection("blogEntries", false)
Then in every view where you want to show the partial view add this:
@section blogEntries {
@Html.Action("_BlogTreeView", "BlogEntries")
}
Problem
Suppose I have a _Layout.cshtml where I render a left sidebar, which is common to every page of my website. Something along these lines - a menu, for example ``` <div id="left-sidebar"> @Html.Action("_MenuView", "LeftSideMenu") </div> ``` A feature I would like to have would be to add another partial view, but only display it in certain sections of the website. For example, in the blog section I may want to display a list of post categories or a treeview of the posts. ``` <div id="left-sidebar"> @Html.Action("_MenuView", "LeftSideMenu") @if ("???") { @Html.Action("_BlogTreeView", "BlogEntries") } </div> ``` How could I do that? I know that I want to display "_BlogTreeView" if the view I'm rendering is returned by BlogController ... where do I go from there?