2013-02-13 53 views
2

我有一個項目有很多大的意見(可編輯的形式;認爲各種調查)。在試用階段,客戶需要一個能夠查看所有表單和元素的角色,但不應該能夠編輯/保存。如何根據角色使網站可編輯或只讀。

一個簡單的解決方法可能是檢查角色並打開/關閉提交按鈕。但我希望有一些非常簡單的方法可以將視圖從可編輯轉換爲只讀,而無需在每個元素上輸入HTML的@readonly(有很多元素,從TextBoxFor,EditorFor到Checkbox和textareas都有)。

該項目利用三個角色:管理員,中心和病人。在有問題的控制器中,我有[授權(角色=「管理員,中心」)],但現在可能需要現在(或需要添加患者),因爲後者需要完整的只讀訪問。

有關如何在不編輯每個模型屬性和/或每個剃鬚刀編輯器的情況下實現這一目標的任何想法?

項目採用MVC3,剃鬚刀,jQuery和jQuery的UI

回答

0

就關閉這個老問題,我會提出我解決的方式。

我不得不重載「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); 
    } 

我不得不這樣做每種類型或表單輸入,顯然我不得不取代視圖中的很多電話。它雖然工作,這是我能想到的最好的解決方案。

相關問題