C# 在ASP.NET MVC 2中修改HTML帮助程序

C# 在ASP.NET MVC 2中修改HTML帮助程序,c#,.net,asp.net-mvc-2,permissions,html-helper,C#,.net,Asp.net Mvc 2,Permissions,Html Helper,我想修改帮助程序,例如: <%= Html.CheckBoxFor(m => m.Current, new { @class = "economicTextBox", propertyName = "Current", onchange = "UseCurrent();UpdateField(this);" })%> m.Current,新的{@class=“economicTextBox”,propertyName=“Current”,onchange=“UseCurren

我想修改帮助程序,例如:

<%= Html.CheckBoxFor(m => m.Current, new { @class = "economicTextBox", propertyName = "Current", onchange = "UseCurrent();UpdateField(this);" })%>
m.Current,新的{@class=“economicTextBox”,propertyName=“Current”,onchange=“UseCurrent();UpdateField(this);”})%%>
要将表示应用程序中权限的另一个字符串作为参数,然后在方法内部,我将根据其权限确定是否返回实际HTML或nothing

我该怎么做

更新2:复选框未呈现为只读

当我调试并检查htmlHelper.CheckBoxFor(expression,mergedHtmlAttributes)的值时,我得到这个值

<input checked="checked" class="economicTextBox" id="Current" name="Current" onchange="UseCurrent();UpdateField(this);" propertyName="Current" readonly="true" type="checkbox" value="true" /><input name="Current" type="hidden" value="false" />


但复选框仍在渲染,允许我更改它并实现完整功能。为什么?

为了做你想做的事情,你需要创建你自己的HTML助手。HTML助手方法只是扩展方法。因此,您可以轻松创建自己的进行适当权限检查的方法,如果通过了,则使用其余参数调用默认的Html.CheckBoxFor


上一篇文章提供了一个创建自定义帮助程序的好例子。

为了实现您的目的,您需要创建自己的HTML帮助程序。HTML助手方法只是扩展方法。因此,您可以轻松创建自己的进行适当权限检查的方法,如果通过了,则使用其余参数调用默认的Html.CheckBoxFor


上一篇文章提供了一个创建自定义帮助程序的好例子。

您可以编写一个自定义帮助程序:

public static MvcHtmlString MyCheckBoxFor<TModel>(
    this HtmlHelper<TModel> htmlHelper,
    Expression<Func<TModel, bool>> expression, 
    string permission, 
    object htmlAttributes
)
{
    if (permission == "foo bar")
    {
        // the user has the foo bar permission => render the checkbox
        return htmlHelper.CheckBoxFor(expression, htmlAttributes);
    }
    // the user has no permission => render empty string
    return MvcHtmlString.Empty;
}
publicstaticmvchtmlstring MyCheckBoxFor(
这个HtmlHelper HtmlHelper,
表情表情,
字符串权限,
对象属性
)
{
如果(权限=“foo-bar”)
{
//用户具有foo-bar权限=>呈现复选框
返回htmlHelper.CheckBoxFor(表达式,htmlAttributes);
}
//用户没有权限=>呈现空字符串
返回MvcHtmlString.Empty;
}
然后:

<%= Html.CheckBoxFor(
    m => m.Current, 
    "some permission string",
    new {  
        @class = "economicTextBox", 
        propertyName = "Current", 
        onchange = "UseCurrent();UpdateField(this);" 
    }) 
%>
m.电流,
“某些权限字符串”,
新{
@class=“economicTextBox”,
propertyName=“当前”,
onchange=“UseCurrent();UpdateField(this);”
}) 
%>

更新:

以下是如何修改HTML帮助程序,以便在用户没有权限时,它呈现只读复选框而不是空字符串:

public static MvcHtmlString MyCheckBoxFor<TModel>(
    this HtmlHelper<TModel> htmlHelper,
    Expression<Func<TModel, bool>> expression,
    string permission,
    object htmlAttributes
)
{
    if (permission == "foo bar")
    {
        // the user has the foo bar permission => render the checkbox
        return htmlHelper.CheckBoxFor(expression, htmlAttributes);
    }
    // the user has no permission => render a readonly checkbox
    var mergedHtmlAttributes = new RouteValueDictionary(htmlAttributes);
    mergedHtmlAttributes["readonly"] = "readonly";
    return htmlHelper.CheckBoxFor(expression, mergedHtmlAttributes);
}
publicstaticmvchtmlstring MyCheckBoxFor(
这个HtmlHelper HtmlHelper,
表情表情,
字符串权限,
对象属性
)
{
如果(权限=“foo-bar”)
{
//用户具有foo-bar权限=>呈现复选框
返回htmlHelper.CheckBoxFor(表达式,htmlAttributes);
}
//用户没有权限=>呈现只读复选框
var mergedHtmlAttributes=新路由值字典(htmlAttributes);
mergedHtmlAttributes[“只读”]=“只读”;
返回htmlHelper.CheckBoxFor(表达式,mergedHtmlAttributes);
}

您可以编写自定义帮助程序:

public static MvcHtmlString MyCheckBoxFor<TModel>(
    this HtmlHelper<TModel> htmlHelper,
    Expression<Func<TModel, bool>> expression, 
    string permission, 
    object htmlAttributes
)
{
    if (permission == "foo bar")
    {
        // the user has the foo bar permission => render the checkbox
        return htmlHelper.CheckBoxFor(expression, htmlAttributes);
    }
    // the user has no permission => render empty string
    return MvcHtmlString.Empty;
}
publicstaticmvchtmlstring MyCheckBoxFor(
这个HtmlHelper HtmlHelper,
表情表情,
字符串权限,
对象属性
)
{
如果(权限=“foo-bar”)
{
//用户具有foo-bar权限=>呈现复选框
返回htmlHelper.CheckBoxFor(表达式,htmlAttributes);
}
//用户没有权限=>呈现空字符串
返回MvcHtmlString.Empty;
}
然后:

<%= Html.CheckBoxFor(
    m => m.Current, 
    "some permission string",
    new {  
        @class = "economicTextBox", 
        propertyName = "Current", 
        onchange = "UseCurrent();UpdateField(this);" 
    }) 
%>
m.电流,
“某些权限字符串”,
新{
@class=“economicTextBox”,
propertyName=“当前”,
onchange=“UseCurrent();UpdateField(this);”
}) 
%>

更新:

以下是如何修改HTML帮助程序,以便在用户没有权限时,它呈现只读复选框而不是空字符串:

public static MvcHtmlString MyCheckBoxFor<TModel>(
    this HtmlHelper<TModel> htmlHelper,
    Expression<Func<TModel, bool>> expression,
    string permission,
    object htmlAttributes
)
{
    if (permission == "foo bar")
    {
        // the user has the foo bar permission => render the checkbox
        return htmlHelper.CheckBoxFor(expression, htmlAttributes);
    }
    // the user has no permission => render a readonly checkbox
    var mergedHtmlAttributes = new RouteValueDictionary(htmlAttributes);
    mergedHtmlAttributes["readonly"] = "readonly";
    return htmlHelper.CheckBoxFor(expression, mergedHtmlAttributes);
}
publicstaticmvchtmlstring MyCheckBoxFor(
这个HtmlHelper HtmlHelper,
表情表情,
字符串权限,
对象属性
)
{
如果(权限=“foo-bar”)
{
//用户具有foo-bar权限=>呈现复选框
返回htmlHelper.CheckBoxFor(表达式,htmlAttributes);
}
//用户没有权限=>呈现只读复选框
var mergedHtmlAttributes=新路由值字典(htmlAttributes);
mergedHtmlAttributes[“只读”]=“只读”;
返回htmlHelper.CheckBoxFor(表达式,mergedHtmlAttributes);
}

这……很有道理。有点举个例子,我会在哪里创建这个方法?从这个角度看,我该怎么称呼它呢?那么我该如何从方法中调用HTML帮助程序呢?这…是有道理的。有点举个例子,我会在哪里创建这个方法?从这个角度看,我该怎么称呼它呢?那么我该如何从该方法调用HTML助手呢?这正是我想要的。但是我应该把这个方法放在哪里呢?在某个额外的类中,然后在aspx视图?@slandau中引用名称空间,您应该将该方法放置在公共静态类中,并且该视图中应该包含声明该类的名称空间,以便它可以解析扩展方法。关于HTML助手的工作原理,您可以看一下以下教程:我在类中遇到此错误:错误17“System.Web.Mvc.HtmlHelper”不包含“CheckBoxFor”的定义,并且找不到扩展方法“CheckBoxFor”接受类型为“System.Web.Mvc.HtmlHelper”的第一个参数(是否缺少using指令或程序集引用?)@slandau,使用System.Web.Mvc.Html添加
。我如何返回相同的控件,而不是返回空的Html字符串,而是变灰并禁用?这正是我想要的。但是我应该将此方法放在哪里?在某个额外的类中,然后在aspx视图中引用名称空间?@slandau,您应该将其放在应指定公共静态类中的方法,以及该类已声明到的命名空间