How to Show or hide controls based on roles - ASP.NET MVC 4 Razor

前端 未结 3 1921
旧巷少年郎
旧巷少年郎 2020-12-29 07:43

I m working on ASP.NET MVC 4 application.I have a dashboard and my users groups will be based on Windows Domain So I am using WIndows Authentication for authenticating users

相关标签:
3条回答
  • 2020-12-29 07:54

    Typically you would want to keep your views as clean as possible with little to no logic. I would suggest moving your role checking logic into a controller action and rendering a partial view based on the users role.

    You can use ChildActions and the Html.Action extension method to get this wired up.

    From MSDN:

    A child action method renders inline HTML markup for part of a view instead of rendering a whole view. Any method that is marked with ChildActionOnlyAttribute can be called only with the Action or RenderAction HTML extension methods.

    In your project, create a new Controller called Dashboard and added a single Action called BuildTable.

    public class DashboardController : Controller
    {
        [ChildActionOnly]
        public ActionResult BuildTable()
        {
            if (Roles.IsUserInRole("Administrator"))
            {
                return PartialView("_AdminTable");
            }
    
            return PartialView("_SupportTable");
        }
    }
    

    Include the following line in the view where you want the dashboard table to appear.

    @Html.Action("BuildTable", "Dashboard")
    
    0 讨论(0)
  • 2020-12-29 07:56

    I have done something similar. The way I did it (may not be the best)

    is to send a boolean back to the view

    in the controller use:

    bool showSection1 = true;
    bool showSection2 = false;
    
    ViewData["showSection1"] = showSection1;
    ViewData["showSection2"] = showSection2;
    /// may be better to use a viewmodel here rather than ViewData
    

    then in the view:

    @if((bool)ViewData["showSection1"])
    {
        @{Html.RenderPartial("section1");}
    }
    @if((bool)ViewData["showSection2"))
    {
        @{Html.RenderPartial("Section2");}
    }
    

    you will need to do the logic to set the boolean the way you want them but this should be a start.

    you could also create a static method that returns the role and then get that value directly from the view. this may be bad form though.

    @if(AppHelper.GetRole().equals("role1")){
        //show the partial
    }
    

    then create a class called AppHelper and a method called GetRole that returns the role of the user.

    0 讨论(0)
  • 2020-12-29 08:04

    You can use Following code for role based checking

    @if(Request.IsAuthenticated)
    
    {
        if(User.IsInRole("Admin"))
        {
         <Ul Class="SubMenuItem">
    
         <li> this menu item is for Admin role</li>
         </Ul>
        }
         if(User.IsInRole("User"))
        {
         <Ul Class="SubMenuItem">
    
         <li> this menu item is for User role</li>
         </Ul>
        }
    }
    @* For unknown user *@
    else
    {
         <Ul Class="SubMenuItem">
             <li> this menu item is for Unknown user</li>
         </Ul>
    }
    
    0 讨论(0)
提交回复
热议问题