Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Html.BeginForm() with custom routes

This is as you surely know the default route:

routes.MapRoute(
    "Default", // Route name
    "{controller}/{action}/{id}", // URL with parameters
    new { controller = "Start", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);

Let's say I use the BeginForm() method like this:

@using (Html.BeginForm("MyAction", "MyController", new { id = 4 }))

This will render the following form tag:

<form method="post" action="/MyController/MyAction/4">

Now, let's say I've made a custom route:

routes.MapRoute(
    "MyCustomRoute", // Route name
    "MyController/{id}/{action}", // URL with parameters
    new { controller = "MyController", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);

When I create a form I'd like it to look like this:

<form method="post" action="/MyController/4/MyAction">

However, if I use BeginForm() as in the example above, I will get a url that matches the default route instead. Is there a way to tell BeginForm() to use my custom route instead of the default one when creating the url for the action? Or does BeginForm() always produce urls that follows the default route pattern?

I'm using asp.net mvc 3 if it matters.

like image 256
haagel Avatar asked Nov 29 '11 09:11

haagel


People also ask

What is HTML BeginForm ()?

"BeginForm()" is an extension method that writes an opening "<form>" tag to the response. "BeginForm()" is an extension method for both HtmlHelper and AjaxHelper classes.

What is the difference between Ajax BeginForm and HTML BeginForm?

Html. BeginForm() will create a form on the page that submits its values to the server as a synchronous HTTP request, refreshing the entire page in the process. Ajax. BeginForm() creates a form that submits its values using an asynchronous ajax request.

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.

Can we use multiple BeginForm in MVC?

Thanks for your help ! Multiple form tags should work fine in MVC unless they are nested.


2 Answers

You can use the Html.BeginRouteForm() method from the HtmlHelper class.

@Html.BeginRouteForm("MyCustomRoute", 
   new { controller = "MyController", action = "MyAction" })
like image 107
Alex Shkor Avatar answered Oct 17 '22 19:10

Alex Shkor


The reason for that is that the routing system picks the first route that matches to build the route. So to circumvent this issue you could reorder your routes but that is quite fragile.

Since the BeginForm method does not add much value you could use the the HTML form element to construct your form and build the action via a @Url.RouteUrl() somehow like this

@Url.RouteUrl("MyCustomRoute", new { controller = "MyController", action = "MyAction" })

Note that I'm using a named route here!

like image 5
saintedlama Avatar answered Oct 17 '22 18:10

saintedlama