233

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...

ACP
  • 34,682
  • 100
  • 231
  • 371

4 Answers4

385

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>
Jason Rowe
  • 6,216
  • 1
  • 33
  • 36
  • 4
    why action and controller are null there? Could you explain it plz? – ACP May 18 '10 at 04:58
  • 7
    I don't know where you want to post the form so I just made them null. – Jason Rowe May 18 '10 at 04:59
  • 8
    Also having the action and controller set to null saves you from having to hard-code them. This is useful if you have your form within a partial view and that partial is used within multiple views like Create and Edit. – Ken Pespisa Apr 22 '11 at 15:23
  • 1
    Where does one find the API or documentation for all of the html helpers? For example where do find out what the parameters stand for? – Zapnologica Jul 14 '13 at 09:59
  • That FormMethod param gets me every time - I go for null/string.empty - my go-to overloads, but can never remember the right order to get the id/class attribute into the form tag. – Shawn J. Molloy Jan 22 '14 at 02:55
  • @JasonRowe Is there a way in which I can add Class and ID ? – Zapnologica Aug 15 '14 at 09:18
  • 2
    @Zapnologica Take a look at this http://msdn.microsoft.com/en-us/library/dd460542%28v=vs.108%29.aspx. The htmlAttributes parameter consists of an object that contains name/value pairs. new { id = "myid", @class="myclass" } – Jason Rowe Aug 15 '14 at 13:05
  • 1
    I really think you should explain in your answer that the two ``null`` parameters are meant to be replaced with the actual controller/action names, since people may think it's allowed to set them to null and that the framework will automatically find the matching post method. – Sasino Nov 03 '19 at 17:25
7

I've added some code to my project, so it's more convenient.

HtmlExtensions.cs:

namespace System.Web.Mvc.Html
{
    public static class HtmlExtensions
    {
        public static MvcForm BeginForm(this HtmlHelper htmlHelper, string formId)
        {
            return htmlHelper.BeginForm(null, null, FormMethod.Post, new { id = formId });
        }

        public static MvcForm BeginForm(this HtmlHelper htmlHelper, string formId, FormMethod method)
        {
            return htmlHelper.BeginForm(null, null, method, new { id = formId });
        }
    }
}

MySignupForm.cshtml:

@using (Html.BeginForm("signupform")) 
{
    @* Some fields *@
}
ADM-IT
  • 129
  • 1
  • 3
6

In System.Web.Mvc.Html ( in System.Web.Mvc.dll ) the begin form is defined like:- Details

BeginForm ( this HtmlHelper htmlHelper, string actionName, string
controllerName, object routeValues, FormMethod method, object htmlAttributes)

Means you should use like this :

Html.BeginForm( string actionName, string controllerName,object routeValues, FormMethod method, object htmlAttributes)

So, it worked in MVC 4

@using (Html.BeginForm(null, null, new { @id = string.Empty }, FormMethod.Post,
    new { @id = "signupform" }))
{
    <input id="TRAINER_LIST" name="TRAINER_LIST" type="hidden" value="">
    <input type="submit" value="Create" id="btnSubmit" />
}
Muhammad Ashikuzzaman
  • 3,075
  • 6
  • 29
  • 53
5

May be a bit late but in my case i had to put the id in the 2nd anonymous object. This is because the 1st one is for route values i.e the return Url.

@using (Html.BeginForm("Login", "Account", new {  ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { id = "signupform", role = "form" }))

Hope this can help somebody :)

Daniaal
  • 882
  • 8
  • 16