开发者

HTML Helper Extension method for Html.Textbox

开发者 https://www.devze.com 2023-02-04 22:10 出处:网络
So I have an extension method for the Html.CheckBoxFor() method that enables the user to pass in an array of permissions like this:

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.CheckBoxForWithPermission(m => m.Current, new string[] { PERMISSIONS.hasICAdvanced }, new { @class = "economicTextBox",开发者_JAVA百科 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("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" 
    }
) %>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号