【问题标题】:Creating html helpers without encoding创建不编码的 html 助手
【发布时间】:2012-03-01 11:19:29
【问题描述】:

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

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

我曾尝试使用 TagBuilder 执行此操作,但遇到问题,因为 MergeAttribute 正在对 html 进行编码。

例如

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

给我 -

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

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

【问题讨论】:

  • 已解决,看看我的新答案,我差点忘了。 =)

标签: asp.net-mvc


【解决方案1】:

--已编辑

我差点忘了,使用 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

【讨论】:

  • 谢谢,您提供的链接非常有帮助,使用 buttonBuilder.MergeAttribute("onClick", "window.location=(" + new JavaScriptSerializer().Serialize(url.Action(action, controller, routeValues)) + ");");为我工作:)
  • 所以,我会再次输入答案。
  • 好吧,如果对你有用,你可以标记为答案,这个答案可能对其他用户有帮助。 =)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-22
  • 1970-01-01
相关资源
最近更新 更多