创建HTML助手而不进行编码 [英] Creating html helpers without encoding

查看:46
本文介绍了创建HTML助手而不进行编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个HTML助手来呈现一个按钮.例如,它需要包含-

I'm trying to create an html helper to render a button. As an example this needs to contain -

<button onClick="window.location='/Users/Edit/admin'">

我尝试使用TagBuilder进行此操作,但是由于MergeAttribute对html进行了编码,因此出现了问题.

I have tried to do this using TagBuilder but am having issues because MergeAttribute is encoding the html.

例如

buttonBuilder.MergeAttribute("onClick","window.location ='" + url.Action(action,controller,routeValues));

buttonBuilder.MergeAttribute("onClick", "window.location='" + url.Action(action, controller, routeValues));

给我-

<button onClick="window.location=&#39;/Users/Edit/admin">

有没有办法做到这一点,使它不被编码?还是我应该使用TagBuilder以外的其他方法?

Is there a way I can do this so it's not encoded? Or should I be using another method other than TagBuilder?

推荐答案

-编辑

我几乎忘记了,使用HtmlDecode:

I almost forget, use the HtmlDecode:

public static class ButtonHelper
{
    public static IHtmlString Button(this HtmlHelper html)
    {
        TagBuilder button = new TagBuilder("button");
        button.MergeAttribute("onClick", "window.location='/Users/Edit/admin'");

        return new HtmlString(System.Web.HttpUtility.HtmlDecode(button.ToString()));
    }
}

或查看: http://forums.asp.net/t/1377957.aspx/1

这篇关于创建HTML助手而不进行编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆