Html.Textbox的HTML Helper扩展方法

时间:2011-01-14 14:39:21

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

所以我有一个Html.CheckBoxFor()方法的扩展方法,它允许用户传入一组权限,如下所示:

<%= 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" })%>

由于这个助手有点不同,我不确定如何格式化该方法。

非常感谢任何帮助。谢谢!

1 个答案:

答案 0 :(得分: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" 
    }
) %>