2011-02-03 68 views
0

所以我有這個文本框的網頁上:TextBoxFor擴展的HtmlHelper

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

它存儲並顯示使用日期選擇器的日期。基本上,根據用戶的權限,我希望這個文本框被禁用/啓用。這很好,我可以做到這一點,但是,我想讓文本框的值=當前日期,如果他們沒有權限更改值。我可以用這個HTML擴展禁用它瓦特/權限:

public static MvcHtmlString TextBoxWithPermission(this HtmlHelper htmlHelper, 
                string name, 
                object value, 
                string[] permissions, 
                object htmlAttributes) 
{ 
    foreach (string permission in permissions) 
    { 
     if (Chatham.Web.UI.Extranet.SessionManager.DisplayUser.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); 
} 

但我怎麼用當前日期填充它,如果它被禁用?

回答

1

如果我正在閱讀此權限,您只是想將當前日期作爲無權限情況下的值傳遞給TextBoxWithPermission傳入的值,而不是傳遞給TextBoxWithPermission中的值?

// the user has no permission => render a readonly checkbox 
var mergedHtmlAttributes = new RouteValueDictionary(htmlAttributes); 
mergedHtmlAttributes["disabled"] = "disabled"; 
return htmlHelper.TextBox(name, DateTime.Now, mergedHtmlAttributes); 
相關問題