简体   繁体   English

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

[英]Error passing array into extension HTML Helper

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. 首先,我有一个HTML Helper扩展方法,该方法通过传入一个额外的字符串来确定我在扩展中检查的权限。 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 CS1501:方法'CheckBoxForWithPermission'的重载没有4个参数

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

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 ?) (出于可读性考虑,只是出于兴趣-不能添加using指令让您仅引用PERMISSIONS.hasICAdvanced吗?)

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

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