user1464139
user1464139

Reputation:

How do I add attributes to HTML.BeginForm with ASP.MVC 4?

I am using the following code:

@using (Html.BeginForm(null, null, 
    new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, 
    new { data-href = "/User/Account/Login"}))

Can someone tell me what's wrong with it. I get an error message pointing to data-href and saying:

Invalid anonymous type member declarator. Anonymous type members must be declared with a member assignment, simple name or member access

Upvotes: 3

Views: 1727

Answers (1)

nemesv
nemesv

Reputation: 139758

The - (dash) is not a valid C# identifier character. Use _ (underscore) and it will be transformed into - so you will get the correct data-href in the generated HTML.

@using (Html.BeginForm(null, null, 
    new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, 
    new { data_href = "/User/Account/Login"}))

Upvotes: 5

Related Questions