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

发布于 2024-10-12 07:09:42 字数 727 浏览 4 评论 0原文

我想修改像这样的帮助器:

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

同时将另一个表示应用程序中的权限的字符串作为参数,然后在方法内部我将确定是否返回实际的 HTML 或不返回任何内容,具体取决于他们的权限。

我该怎么做?

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

当我调试并检查 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" />

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

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.

How would I do this?

UPDATE 2: Checkbox not rendering as readonly

When I debug and check the value of htmlHelper.CheckBoxFor(expression, mergedHtmlAttributes)._value, I get this

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

伴随着你 2024-10-19 07:09:42

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

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

然后:

<%= Html.CheckBoxFor(
    m => m.Current, 
    "some permission string",
    new {  
        @class = "economicTextBox", 
        propertyName = "Current", 
        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);
}

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:

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);
}
请帮我爱他 2024-10-19 07:09:42

为了执行您想要的操作,您需要创建自己的 HTML Helper。 HTML Helper 方法只是扩展方法。因此,您可以轻松创建自己的执行适当的权限检查,然后如果通过,则使用其余参数调用默认的 Html.CheckBoxFor。

前面的问题有一个创建自定义帮助器的不错的示例。

In order to do what you want, you need to create you own HTML Helper. The HTML Helper methods are just extension methods. 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.

This previous question has a decent example of creating custom helpers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文