将数组传递到扩展 HTML Helper 时出错
因此,我在创建此方法并使其发挥作用方面得到了很多帮助……但由于这是一个不同的人,我想我会从中提出另一个问题。
首先,我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这看起来像问题:
我认为应该是:
您当前的代码正在尝试传递一个具有一个权限的数组,然后传递另一个字符串......而大概您意味着传递一个具有两种权限的数组。
话虽如此,这两个权限在这里是相同的......这看起来也是无意的。您尝试使用什么权限?
(只是出于兴趣,为了可读性 - 你不能添加一个 using 指令来让你只引用
PERMISSIONS.hasICAdvanced
吗?)This bit looks like the problem:
I think that should be:
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
?)