0

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

I thought about checking the url and just calling Html.RenderPartial within an if block but that seems clunky.

Is there a way to control this with a controller?

Christopher Johnson
  • 2,629
  • 7
  • 39
  • 70
  • What are you trying to do? Just highlight the current navigation node? – Erik Funkenbusch Aug 23 '11 at 02:36
  • no, I'm going to offer different navigation links depending on which page the user is on. – Christopher Johnson Aug 23 '11 at 02:38
  • 1
    That seems really confusing. Navigation's purpose is to let the user know where they are. If you change it on them on every page, they are lost. Imagine if you were trying to find your way using a map and every time you looked at it, the map was replaced with a different one. – Erik Funkenbusch Aug 23 '11 at 02:40

1 Answers1

1

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.

DMulligan
  • 8,993
  • 6
  • 33
  • 34