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(['_link', 'someurl'])">
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('some test');">
<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.
精彩评论