Html.Textbox 的 HTML Helper 扩展方法

发布于 2024-10-11 17:37:35 字数 1914 浏览 4 评论 0原文

因此,我有一个 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" })%>

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

任何帮助将不胜感激。谢谢!

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", 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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柒七 2024-10-18 17:37:35
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" 
    }
) %>
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" 
    }
) %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文