asp.net MVC3 razor: display actionlink based on user role

后端 未结 6 1092
臣服心动
臣服心动 2020-11-30 18:57

I\'m new to MVC. I want to be able to hide some actionlinks for some users. Say I have a \"create\" actionlink which I only want administrators to see and click. I want to u

6条回答
  •  庸人自扰
    2020-11-30 19:10

    I have in the past created a helper function to only return output when a criteria is met like this:

    public static MvcHtmlString If(this MvcHtmlString value, bool evaluation)
    {
         return evaluation ? value : MvcHtmlString.Empty;
    }
    

    so you can use this:

    @Html.ActionLink("Create New", "Create").If(User.IsInRole("Administrators"))
    

    This way it is legible and short

提交回复
热议问题