Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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"> 
like image 532
user426306 Avatar asked Sep 10 '10 09:09

user426306


People also ask

What is HTML BeginForm ()?

BeginForm(HtmlHelper) Writes an opening <form> tag to the response. The form uses the POST method, and the request is processed by the action method for the view. BeginForm(HtmlHelper, String, String, Object, FormMethod, Object)

Why we use HTML BeginForm?

Html. BeginForm is the Html Helper Extension Method that is used for creating and rendering the form in HTML. This method makes your job easier in creating form. Here, is the method to create a form using Html.

How do you pass ID in BeginForm?

BeginForm("action","controller", new { Id = 12345 }, FormMethod. Post); Reversing the third and fourth parameters will result in the Id being treated as an attribute instead of a route value.


1 Answers

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

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

like image 145
BritishDeveloper Avatar answered Sep 23 '22 08:09

BritishDeveloper