How can I name a form with Html.BeginForm()?

asp.net MvcRazor

asp.net Mvc Problem Overview


How do I give a name to a form in ASP.NET MVC using Html.BeginForm()? I want only the name, not the action or controller name because I want to post it through Javascript. I think it should be something like Html.BeginForm(id = "frm").

I tried the following:

Html.BeginForm(null,null,new{id="frm",name="frm})

Html.BeginForm(new{@id="frm",@name="frm})

But the above code produces output like this:

<form action="/Main/Index/Id?name=Id" method="post">

asp.net Mvc Solutions


Solution 1 - asp.net Mvc

Html.BeginForm(null, null, FormMethod.Get, new { name = "frm", id = "frm" })

You'll need to catch the form submit with your JavaScript

Solution 2 - asp.net Mvc

Using MVC2, this is what worked for me:

<% using (Html.BeginForm("Index", "Home", null, FormMethod.Post, new {@id = "myForm", @name = "myForm"})) {%>

Solution 3 - asp.net Mvc

@HTML.BeginForm("Target-ViewName where you want to post the page","Controller Name",new {@name="Name of the Form", id="ID of the Form"}) 
{ //form here
}

Solution 4 - asp.net Mvc

Taken from this answer: https://stackoverflow.com/questions/878330/how-to-pass-in-id-with-html-beginform

Can you not just do:

Html.BeginForm(new {@id="Id", @name="Id"}); 

It can pay to search SO for answers as I've found many things I want to ask have already been encountered.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser426306View Question on Stackoverflow
Solution 1 - asp.net MvcBritishDeveloperView Answer on Stackoverflow
Solution 2 - asp.net MvcSteven ArchibaldView Answer on Stackoverflow
Solution 3 - asp.net MvcBrahmaiah Chowdary MurakondaView Answer on Stackoverflow
Solution 4 - asp.net MvcPaul HadfieldView Answer on Stackoverflow