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?
Rendering a Partial View You can render the partial view in the parent view using the HTML helper methods: @html. Partial() , @html. RenderPartial() , and @html. RenderAction() .
RenderPartial function to render Partial View in ASP.Net MVC Razor. The data will be fetched from database using Entity Framework and then the Partial View will be rendered using the @Html. RenderPartial function in ASP.Net MVC Razor.
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")
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With