16

I am new to MVC.

How I can add id and css class in MVC Html.BeginForm().

This is html form.

<form id="frmId" class="frmStyle">
    ...
</form>

I tried this but got error.

@Html.BeginForm("actionName", "controllerName", new {id="frmId", class = "frmStyle"})

1 Answer 1

36

We add as part of htmlAttributes.

@Html.BeginForm("actionName", "controllerName", FormMethod.Post,
 new {id="frmId", @class = "frmStyle"})
Sign up to request clarification or add additional context in comments.

3 Comments

why does id need to add a @ before it?
The @ character before the property names escapes them, so as not to clash with C# keywords. class is a keyword. You would get a compiler error if you didn't escape them. Not entirely sure why id is escaped in this answer though; AFAIK it's not a keyword.
@ is only needed for @class

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.