ASP.NET MVC - Form Value display by role

前端 未结 3 1988
忘掉有多难
忘掉有多难 2021-01-03 12:55

I\'m looking for an ideal way for my input forms to either show as a textbox (editable), label (readonly), or hidden (no access) based on the roles. I know I could have a d

3条回答
  •  无人及你
    2021-01-03 13:24

    You could have a custom Html Control which checks the user's role and then decide what to do.

    I did this with Actionlinks, created Html.ActionLinkSecured

    Namespace System.Web.Mvc.Html
    {
    public static class HtmlHelperExtensions
    {
        public static string ActionLinkSecured(this HtmlHelper htmlHelper, string linkText, string action, string controller, object routeValues, bool showDisabled)
        {
          //check if user is logged in or whatever you wanna check
          //if ok
          return htmlHelper.ActionLink(...);
          //else
          return linkText
        }
    }
    

    You can have whatever you want... text, textbox, disabled textbox... :)

提交回复
热议问题