Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 3 如何使网站可编辑而不是只读取决于角色。_Asp.net Mvc 3_Authorization_Readonly_Roleprovider - Fatal编程技术网

Asp.net mvc 3 如何使网站可编辑而不是只读取决于角色。

Asp.net mvc 3 如何使网站可编辑而不是只读取决于角色。,asp.net-mvc-3,authorization,readonly,roleprovider,Asp.net Mvc 3,Authorization,Readonly,Roleprovider,我有一个有很多大视图的项目(可编辑表单;各种各样的调查)。在试用阶段,客户希望角色可以查看所有表单和元素,但不能编辑/保存 一个简单的解决方法可能是检查角色并切换提交按钮的开/关。但我希望有一种非常简单的方法可以将视图从可编辑转换为只读,而不必在每个元素上输入HTML的@readonly(有上千个元素,从TextBoxFor、EditorFor到Checkbox和textareas) 该项目使用三个角色:管理员、中心和患者。在所讨论的控制器中,我有[Authorize(Roles=“Admin,

我有一个有很多大视图的项目(可编辑表单;各种各样的调查)。在试用阶段,客户希望角色可以查看所有表单和元素,但不能编辑/保存

一个简单的解决方法可能是检查角色并切换提交按钮的开/关。但我希望有一种非常简单的方法可以将视图从可编辑转换为只读,而不必在每个元素上输入HTML的@readonly(有上千个元素,从TextBoxFor、EditorFor到Checkbox和textareas)

该项目使用三个角色:管理员、中心和患者。在所讨论的控制器中,我有[Authorize(Roles=“Admin,center”)],但这可能需要立即执行(或者我需要添加患者),因为后者需要完全的只读访问

关于如何在不编辑每个模型属性和/或每个razor编辑器的情况下实现这一点,有什么想法吗


该项目使用mvc3、razor、jquery和jquery ui来解决这个老问题,我将介绍解决问题的方法

我不得不重载“TextBoxFor”、“CheckBoxFor”(等等),在方法的末尾添加一个只读/禁用的布尔参数。Exmaple如下:

    //Overload TextBoxFor with a "disabled" parameter. For use with readonly-roles
    public static IHtmlString TextBoxForRo<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, object htmlAttributes, bool disabled)
    {
        var attributes = new RouteValueDictionary();

        if (htmlAttributes != null)
        {
            foreach (System.ComponentModel.PropertyDescriptor property in System.ComponentModel.TypeDescriptor.GetProperties(htmlAttributes))
            {
                attributes.Add(property.Name.Replace('_', '-'), property.GetValue(htmlAttributes));
            }
        }

        if (disabled)
        {
            attributes["disabled"] = "disabled";
        }
        return htmlHelper.TextBoxFor(expression, attributes);
    }  
//使用“disabled”参数重载TextBoxFor。用于只读角色
公共静态IHtmlString TEXTBOXFORO(此HtmlHelper HtmlHelper、表达式、对象htmlAttributes、布尔禁用)
{
var attributes=新的RouteValueDictionary();
如果(htmlAttributes!=null)
{
foreach(System.ComponentModel.PropertyDescriptor属性在System.ComponentModel.TypeDescriptor.GetProperties(htmlAttributes))中)
{
attributes.Add(property.Name.Replace(“”,“-”),property.GetValue(htmlAttributes));
}
}
如果(禁用)
{
属性[“已禁用”]=“已禁用”;
}
返回htmlHelper.TextBoxFor(表达式、属性);
}  
我必须对每个类型或表单输入都这样做,显然我必须替换视图中的许多调用。这是我能想到的最好的解决办法