5

I only want to add the formId in the beginForm()

If i try using
Html.BeginForm(null, null, FormMethod.Post, new {@id="Id"})

then the Html generated is
<form action="/newquestion/payment/b9f88f80-f31f-4144-9066-55384c9f1cfc" ... >

i don't know how that action url is generated so i tried,
Html.BeginForm(new {@id="Id"})
but then the action url looks like this
<form action="/newquestion/payment/Id... >

In both the cases the action url is not what it should be and it is not hitting the post action of the controller.

Sam Axe
  • 33,313
  • 9
  • 55
  • 89
Amit
  • 63
  • 1
  • 1
  • 10
  • MVC uses the router to determine the URL's. What is the URL supposed to be? – P.Brian.Mackey Nov 26 '13 at 03:29
  • I don't want to set a fix controller and action in the BeginForm() because i want it to call the POST action based on the GET action. For example if i call payment (GET) then it should call payment (POST) and if i call Bundle (GET) then it should call Bundle (POST) action. This was working fine until now because i only had Html.BeginForm() but now i want to add the formId and that is breaking the behavior. – Amit Nov 26 '13 at 05:55

3 Answers3

5

When you are trying to generate a route, such as with BeginForm, MVC will do its best to include things that you might need.

If you're at domain.com/Home/Index/b9f88f80-f31f-4144-9066-55384c9f1cfc and you use the code that you have provided than the form will be generated with the action, controller and route values as it finds them.

controller / action / id
/Home      / Index  / b9f88f80-f31f-4144-9066-55384c9f1cfc

One way to get around this is to force id to be nothing (such as an empty string).

Observe:

@using (Html.BeginForm(null, null, new { @id = string.Empty },
    FormMethod.Post, new { @id = "Id" }))
{

}

The new { @id = string.Empty } is an anonymous object that represents the route values.

Rowan Freeman
  • 15,724
  • 11
  • 69
  • 100
1

Change @id to id.

 @using (Html.BeginForm("Index", "Home", FormMethod.Post, new { id = "MyForm1" }))    
    {
        <input id="submit" type="submit" value="Search" />
    }
Yousuf
  • 3,105
  • 7
  • 28
  • 38
1

Because of in System.Web.Mvc.Html ( in System.Web.Mvc.dll ) the begin form is defined like bellowed:-

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

Means like this : Html.BeginForm( string actionName, string controllerName, object routeValues, FormMethod method, object htmlAttributes)

So, it worked for me in MVC 4

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