Role Based Navigation

前端 未结 1 1470
广开言路
广开言路 2020-12-15 00:31

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

相关标签:
1条回答
  • 2020-12-15 00:59

    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");
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题