开发者

ASP.NET MVC - Form Value display by role

开发者 https://www.devze.com 2022-12-08 11:02 出处:网络
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 different view for each type o

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 different view for each type of role but I was hoping that there's some new goodness out there that would p开发者_StackOverflow中文版revent me from having to do 80 views.


Really it all depends on where you want to set your security related meta data. What do you want to to do? Decorate your view models with attributes? Use Dynamic Data meta buddy classes? Fluent configuration ala StructureMap/FluentNhibernate?

One way to do it using MVC Preview 2 or the InputBuilder project from lostechies.com and attribute decorations is to override UIHint and supply your own UIHint values back to template builders:

public class RoleUI : UIHintAttribute
{
    public RoleUI( string roles ) : base("","")
    {
        if( HttpContext.WhereverTheRoleStuffIs == "Admin" ) 
        //could be Session["CurrentUser"] too
        {
            this.UIHint = "Input";
        }

        this.UIHint = "Label";
    }

}

So your view model:

public class AwesomeModel
{
    [RoleUI("Admin")]
    public string FirstName { get; set; }
}

So now when the code goes to grab the partials for you it knows whether to display it in an input tag or just a lable or placeholder.


I think you're best having a readonly view and and editable view I'm afraid.

And input form that is readonly does not sound right, unless this is at a field level when you might be onto something.


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... :)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号