TextBoxFor扩展名HTMLHelper

时间:2011-02-03 18:55:32

标签: c# .net asp.net-mvc html-helper

所以我在页面上有这个文本框:

<%= 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 个答案:

答案 0 :(得分:1)

如果我正确读取此权限,您只想将当前日期作为无权限情况下的值传递,而不是传递给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);