ASP.NET MVC - 按角色表数值显示 [英] ASP.NET MVC - Form Value display by role

查看:137
本文介绍了ASP.NET MVC - 按角色表数值显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在寻找一种理想的方式为我输入的形式要么显示为基于角色的文本框(可编辑),标签(只读),或隐藏的(没有访问)。我知道我可以有不同的看法每种类型的角色,但我希望有一些新的善良,在那里,将prevent我不必做80的看法。

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 prevent me from having to do 80 views.

推荐答案

真的一切都取决于你在哪里要设置安全相关的元数据。那你想干什么?带属性的装饰您的视图模型?使用动态数据元哥们类?流利的配置ALA StructureMap / FluentNhibernate?

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?

要做到这一点使用MVC preVIEW 2或lostechies.com的InputBuilder项目和属性的装饰品的一种方法是重写UIHint并提供自己的UIHint值回模板建设者:

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; }
}

所以,现在当code去抓住它知道是否在输入标签或只是一个拉布勒或占位符显示它。你谐音

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.

这篇关于ASP.NET MVC - 按角色表数值显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆