user426306
user426306

Reputation: 903

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

Upvotes: 85

Views: 88954

Answers (4)

@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
}

Upvotes: 0

Steven Archibald
Steven Archibald

Reputation: 91

Using MVC2, this is what worked for me:

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

Upvotes: 9

BritishDeveloper
BritishDeveloper

Reputation: 13349

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

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

Upvotes: 145

Paul Hadfield
Paul Hadfield

Reputation: 6136

Taken from this answer: 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.

Upvotes: -5

Related Questions