【问题标题】:How to make a site editable vs. readonly depending on Roles.如何根据角色使网站可编辑与只读。
【发布时间】:2013-01-28 22:35:45
【问题描述】:

我有一个包含许多大视图的项目(可编辑的表单;考虑各种调查)。在试点阶段,客户需要一个可以查看所有表单和元素但不能编辑/保存的角色。

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

该项目使用三个角色:管理员、中心和患者。在有问题的控制器中,我有 [Authorize(Roles="Admin,Centre")],但这可能需要现在进行(或者我需要添加 Patient),因为后者需要完整的只读访问权限。

关于如何在不编辑每个模型属性和/或每个 razor-editor 的情况下使其工作的任何想法?

项目使用 mvc3、razor、jquery 和 jQuery-ui

【问题讨论】:

    标签: asp.net-mvc-3 authorization readonly roleprovider


    【解决方案1】:

    为了结束这个老问题,我将介绍我解决它的方法。

    我不得不重载“TextBoxFor”、“CheckBoxFor”(等等),在方法的末尾添加一个用于 readOnly/disabled 的布尔参数。示例如下:

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

    我必须对每种类型或表单输入都这样做,显然我必须替换视图中的很多调用。它虽然有效,但这是我能想到的最佳解决方案。

    【讨论】:

      猜你喜欢
      • 2018-06-13
      • 1970-01-01
      • 2019-11-09
      • 2011-04-24
      • 1970-01-01
      • 1970-01-01
      • 2019-11-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多