Martin McMahon
Martin McMahon

Reputation: 333

Creating html helpers without encoding

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

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

e.g.

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

gives me -

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

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

Upvotes: 2

Views: 2377

Answers (1)

Vinicius Ottoni
Vinicius Ottoni

Reputation: 4677

--EDITED

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

Or look it: http://forums.asp.net/t/1377957.aspx/1

Upvotes: 2

Related Questions