简体   繁体   English

Html.Textbox的HTML Helper扩展方法

[英]HTML Helper Extension method for Html.Textbox

So I have an extension method for the Html.CheckBoxFor() method that enables the user to pass in an array of permissions like this: 因此,我为Html.CheckBoxFor()方法提供了一个扩展方法,该方法使用户能够传递如下所示的权限数组:

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

The method looks like 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);
        }

Basically, I want to create the exact same thing except for an Html.TextBox method that we currently call like this: 基本上,除了我们当前这样调用的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" })%>

Since this helper is a little bit different I'm not really sure how to format the method. 由于此助手有点不同,所以我不确定如何格式化该方法。

Any help would be greatly appreciated. 任何帮助将不胜感激。 Thanks! 谢谢!

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);
}

and then: 接着:

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

and if you want to have the format you could use untyped helpers: 如果您希望使用这种格式,则可以使用无类型的帮助器:

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);
}

and call like this: 并这样调用:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM