2011-01-14 65 views
0

所以,我有一個擴展方法,用於使用戶在權限的陣列這樣傳遞Html.CheckBoxFor()方法:HTML輔助擴展方法Html.Textbox

<%= Html.CheckBoxForWithPermission(m => m.Current, new string[] { PERMISSIONS.hasICAdvanced }, new { @class = "economicTextBox", propertyName = "Current", onchange = "UseCurrent();UpdateField(this);" })%> 

的方法,看起來像這樣的:

public static MvcHtmlString CheckBoxForWithPermission<TModel>(
                  this HtmlHelper<TModel> htmlHelper, 
                  Expression<Func<TModel, bool>> expression, 
                  string[] permissions, 
                  object htmlAttributes 
                 ) 
     { 
      foreach (string permission in permissions) 
      { 
       if (Chatham.Web.UI.Extranet.SessionManager.PhysicalUser.IsInRole(permission)) 
       { 
        // the user has the permission => render the checkbox 
        return htmlHelper.CheckBoxFor(expression, htmlAttributes); 
       } 
      } 
      // the user has no permission => render a readonly checkbox 
      var mergedHtmlAttributes = new RouteValueDictionary(htmlAttributes); 
      mergedHtmlAttributes["disabled"] = "disabled"; 
      return htmlHelper.CheckBoxFor(expression, mergedHtmlAttributes); 
     } 

基本上,我想創建除了一個Html.TextBox方法完全一樣的事情,我們目前這樣調用:

<%= Html.TextBox("RateTimeStamp", Model.RateTimeStamp.HasValue ? Model.RateTimeStamp.Value.ToString("dd-MMM-yyyy") : "", new { @class = "economicTextBox", propertyName = "RateTimeStamp", onchange = "parseAndSetDt(this);", dataType = "Date" })%> 

由於這個幫手有點不同,我不確定如何格式化該方法。

任何幫助將不勝感激。謝謝!

回答

2
public static MvcHtmlString TextBoxForWithPermission<TModel>(
    this HtmlHelper<TModel> htmlHelper, 
    Expression<Func<TModel, bool>> expression, 
    string[] permissions, 
    object htmlAttributes 
) 
{ 
    foreach (string permission in permissions) 
    { 
     if (Chatham.Web.UI.Extranet.SessionManager.PhysicalUser.IsInRole(permission)) 
     { 
      // the user has the permission => render the textbox 
      return htmlHelper.TextBoxFor(expression, htmlAttributes); 
     } 
    } 

    // the user has no permission => render a readonly checkbox 
    var mergedHtmlAttributes = new RouteValueDictionary(htmlAttributes); 
    mergedHtmlAttributes["disabled"] = "disabled"; 
    return htmlHelper.TextBoxFor(expression, mergedHtmlAttributes); 
} 

然後:

<%= Html.TextBoxForWithPermission(
    x => x.RateTimeStamp, 
    new string[] { PERMISSIONS.hasICAdvanced }, 
    new { 
     @class = "economicTextBox", 
     propertyName = "RateTimeStamp", 
     onchange = "parseAndSetDt(this);", 
     dataType = "Date" 
    } 
) %> 

,如果你想擁有你可以使用非類型化的助手的格式爲:

public static MvcHtmlString TextBoxWithPermission<TModel>(
    this HtmlHelper<TModel> htmlHelper, 
    string name, 
    object value, 
    string[] permissions, 
    object htmlAttributes 
) 
{ 
    foreach (string permission in permissions) 
    { 
     if (Chatham.Web.UI.Extranet.SessionManager.PhysicalUser.IsInRole(permission)) 
     { 
      // the user has the permission => render the textbox 
      return htmlHelper.TextBox(name, value, htmlAttributes); 
     } 
    } 

    // the user has no permission => render a readonly checkbox 
    var mergedHtmlAttributes = new RouteValueDictionary(htmlAttributes); 
    mergedHtmlAttributes["disabled"] = "disabled"; 
    return htmlHelper.TextBox(name, value, mergedHtmlAttributes); 
} 

,並呼籲像這樣:

<%= Html.TextBoxWithPermission(
    "RateTimeStamp", 
    Model.RateTimeStamp.HasValue 
     ? Model.RateTimeStamp.Value.ToString("dd-MMM-yyyy") 
     : "", 
    new string[] { PERMISSIONS.hasICAdvanced }, 
    new { 
     @class = "economicTextBox", 
     propertyName = "RateTimeStamp", 
     onchange = "parseAndSetDt(this);", 
     dataType = "Date" 
    } 
) %>