简体   繁体   English

修改ASP.NET MVC 2中的HTML帮助器

[英]Modify the HTML Helpers in ASP.NET MVC 2

I want to modify helpers such as this one: 我想修改这样的助手:

<%= Html.CheckBoxFor(m => m.Current, new { @class = "economicTextBox", propertyName = "Current", onchange = "UseCurrent();UpdateField(this);" })%>

to also take as a parameter another string that signifies a permission in the app, and then INSIDE the method I would determine whether or not to return the actual HTML or nothing, depending on their permission. 还将另一个表示应用程序中权限的字符串作为参数,然后在该方法的INSIDE内,根据其权限确定是否返回实际的HTML或不返回任何内容。

How would I do this? 我该怎么做?

UPDATE 2: Checkbox not rendering as readonly 更新2:复选框不呈现为只读

When I debug and check the value of htmlHelper.CheckBoxFor(expression, mergedHtmlAttributes)._value, I get this 当我调试并检查htmlHelper.CheckBoxFor(expression,mergedHtmlAttributes)._ value的值时,我得到了

<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" />

but the checkbox is still rendering allowing me to change it and achieve full functionality. 但该复选框仍在渲染,允许我对其进行更改并实现全部功能。 Why? 为什么?

You could write a custom helper: 您可以编写一个自定义帮助器:

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

and then: 接着:

<%= Html.CheckBoxFor(
    m => m.Current, 
    "some permission string",
    new {  
        @class = "economicTextBox", 
        propertyName = "Current", 
        onchange = "UseCurrent();UpdateField(this);" 
    }) 
%>

UPDATE: 更新:

Here's how you could modify the HTML helper so that it renders a readonly checkbox instead of an empty string if the user has no permissions: 您可以按照以下方法修改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);
}

In order to do what you want, you need to create you own HTML Helper. 为了执行您想要的操作,您需要创建自己的HTML Helper。 The HTML Helper methods are just extension methods. HTML Helper方法只是扩展方法。 As such you can easily create your own that does the proper permission checking and then if it passes, call the default Html.CheckBoxFor with the rest of the parameters. 这样,您可以轻松创建自己的设备,以进行适当的权限检查,然后通过权限检查,并使用其余参数调用默认的Html.CheckBoxFor。

This previous question has a decent example of creating custom helpers. 前面的问题有一个创建自定义助手的不错的例子。

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

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