Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to unencode attribute when calling Html.BeginForm

Tags:

asp.net-mvc

I'm trying to hook into Google Analytics and have this:

@using (Html.BeginForm("Register", "Account", FormMethod.Post, 
        new { id = "account-register-form", 
        onsubmit = "_gaq.push(['_link', 'someurl'])" }))

My rendered view then looks like this:

<form action="/Account/Register/Basic" id="account-register-form" method="post" 
onsubmit="_gaq.push([&#39;_link&#39;, &#39;someurl&#39;])">

I have tried Html.Raw("_gaq.push(['_link', 'someurl'])") but this does not work, because I think BeginForm does the encoding.

like image 561
adriaanp Avatar asked Apr 28 '26 19:04

adriaanp


1 Answers

Code works fine if don't turn off encoding but you can turn off attribute encoding by creating a class like this:

public class HtmlAttributeEncodingNot : System.Web.Util.HttpEncoder
{
protected override void HtmlAttributeEncode(string value, System.IO.TextWriter output)
{
    output.Write(value);
}
}

and adding this to web.config under :

<httpRuntime encoderType="HtmlAttributeEncodingNot"/>
like image 101
c0demaster Avatar answered May 01 '26 08:05

c0demaster