How to add ID property to Html.BeginForm() in asp.net mvc?

ACP picture ACP · May 18, 2010 · Viewed 190.5k times · Source

I want to validate my form using jquery but it doesn't have an ID property as of now how to add it to the form in asp.net mvc? I am using this...

<% using (Html.BeginForm()) {%>

and my jquery validator plugin takes this,

var validator = $("#signupform").validate({

Now i want to give id as signupform... Any suggestion...

Answer

Jason Rowe picture Jason Rowe · May 18, 2010

This should get the id added.

ASP.NET MVC 5 and lower:

<% using (Html.BeginForm(null, null, FormMethod.Post, new { id = "signupform" }))
   { } %>

ASP.NET Core: You can use tag helpers in forms to avoid the odd syntax for setting the id.

<form asp-controller="Account" asp-action="Register" method="post" id="signupform" role="form"></form>