I\'ve been trying to come up with a way to create a dynamic role based navigation solution for a project that I am working on.
The navigation should display only li
SOLUTION
As suggested, I created a ChildAction called Menu and partial views for each role. Inside the action I do some role checking using some conditional statements and render the appropriate view.
This keeps the conditional statements out of the views which makes it a lot cleaner solution.
I'm sure there are a few things that could be done to tidy it up and I will continue trying to improve it.
Here is the solution I used.
In the layout view where I wanted the menu to appear I used this.
@Html.Action("Menu", "Navigation")
Then I created a controller called Navigation and added a single Action called Menu.
public class NavigationController : Controller
{
[ChildActionOnly]
public ActionResult Menu()
{
if (Roles.IsUserInRole("Administrator"))
{
return PartialView("_NavigationAdmin");
}
return PartialView("_NavigationPublic");
}
}