简体   繁体   中英

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

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 ?)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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