将数组传递到扩展 HTML Helper 时出错

发布于 2024-10-11 14:22:13 字数 1881 浏览 5 评论 0原文

因此,我在创建此方法并使其发挥作用方面得到了很多帮助……但由于这是一个不同的人,我想我会从中提出另一个问题。

首先,我有一个 HTML Helper 扩展方法,它通过传入一个额外的字符串来确定我在扩展中检查的权限。我现在想传递一个字符串数组(权限),并在方法中循环它们,但我收到此错误:

CS1501:方法“CheckBoxForWithPermission”没有重载需要 4 个参数

这是助手的调用:

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

并且这是实际的方法:

// CHECKBOX WITH PERMISSIONS
        // WITHOUT -- READONLY
        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);
        }

So I had plenty of help on here creating this method and getting it working...but since this is someone different I thought I would make another question out of it.

First I had an HTML Helper extension method working by passing in an extra string to determine a permission which I checked for in the extension. I now want to pass in an array of strings (permissions), and loop through them in the method, but I am getting this error:

CS1501: No overload for method 'CheckBoxForWithPermission' takes 4 arguments

Here is the call of the helper:

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

And here is the actual method:

// CHECKBOX WITH PERMISSIONS
        // WITHOUT -- READONLY
        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);
        }

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

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

发布评论

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

评论(1

挽清梦 2024-10-18 14:22:13

这看起来像问题:

new string[] {
    Chatham.Web.Business.Definitions.Constants.PERMISSIONS.hasICAdvanced
}, 
Chatham.Web.Business.Definitions.Constants.PERMISSIONS.hasICAdvanced

我认为应该是:

new string[] {
    Chatham.Web.Business.Definitions.Constants.PERMISSIONS.hasICAdvanced,    
    Chatham.Web.Business.Definitions.Constants.PERMISSIONS.hasICAdvanced
}

您当前的代码正在尝试传递一个具有一个权限的数组,然后传递另一个字符串......而大概您意味着传递一个具有两种权限的数组。

话虽如此,这两个权限在这里是相同的......这看起来也是无意的。您尝试使用什么权限?

(只是出于兴趣,为了可读性 - 你不能添加一个 using 指令来让你只引用 PERMISSIONS.hasICAdvanced 吗?)

This bit looks like the problem:

new string[] {
    Chatham.Web.Business.Definitions.Constants.PERMISSIONS.hasICAdvanced
}, 
Chatham.Web.Business.Definitions.Constants.PERMISSIONS.hasICAdvanced

I think that should be:

new string[] {
    Chatham.Web.Business.Definitions.Constants.PERMISSIONS.hasICAdvanced,    
    Chatham.Web.Business.Definitions.Constants.PERMISSIONS.hasICAdvanced
}

Your current code is trying to pass in an array with one permission, and then another single string... whereas presumably you meant to pass in an array with both permissions.

Having said that, both permissions are the same here... that looks unintentional too. What permissions were you trying to use?

(Just out of interest, for the sake of readability - can't you add a using directive to let you just refer to PERMISSIONS.hasICAdvanced?)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文