开发者

dynamically render partial view on _layout.cshtml mvc 3

开发者 https://www.devze.com 2023-03-30 03:42 出处:网络
I want to display different navigation links based on the page that is loaded in my _layout.cshtml file.

I want to display different navigation links based on the page that is loaded in my _layout.cshtml file.

I thought about checking t开发者_开发技巧he url and just calling Html.RenderPartial within an if block but that seems clunky.

Is there a way to control this with a controller?


If you truly need different navigation links on different pages then I think you should specify different layouts pages on these separate pages. These different layouts should then specify your _layout as their layout, making it the master layout

Ex: _navlinks1.cshtml

@{
   Layout = "_layout"
 }

@RenderBody()

@section navlinks
{
    @*create navlinks specific to current page*@
}

Then in your _layout page you can put @RenderSection("navlinks", false) where you want the navigation links to go.

But, if for some reason you need a distinct set of navigation links for every single page, then putting navigation links in your layout might not make sense. Might be better off having all your models inherit a base model with a list of items containing navigation link data. Then call a partial view that processes this data into the correct links in your views.

0

精彩评论

暂无评论...
验证码 换一张
取 消