开发者

How to unencode attribute when calling Html.BeginForm

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.


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


You don't need to unencode anything. What you have is perfectly valid markup and working javascript, as seen in the following live demo:

<form action="#" method="get" onsubmit="alert(&#39;some test&#39;);">
    <input type="submit" value="OK" />
</form>

So keep your code as is.


If you don't have to use the Html.BeginForm then use can use the code snippet below to solve your formatting issue.

<form action="@Url.Action("Register", "Account")" 
    method="POST" id="account-register-form" 
    onsubmit="_gaq.push(['_link', 'someurl'])">
</form>

This outputs the html you require.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜