ASP.NET BeginForm does not apply the route mapping when making the request

4

I have a route problem (again), I use ASP.NET MVC5, and I have the following route mapped:

routes.MapRoute(
    "Search_route",
    "Home/Search/{search_input}/{search_input_category}",
    new { 
        controller = "Home",
        action = "Search",
        search_input = UrlParameter.Optional,
        search_input_category = UrlParameter.Optional
    }

);

If I apply: Url.Action("Search", "Home", new { search_input = "casa", search_input_category = "vetores" }) my return will be: / Home / Search / home / vectors . But when trying to use this route in the following form:

@using (Html.BeginForm("Search", "Home", FormMethod.Get, new { @class = "search-form" })){
         //Todos os campos de input devidamente nomeados
}
search_input = Home & search_input_category = vectors That is, the route is correct, but it is not being applied to <form> for some reason . Is it possible to apply the route to the form somehow?

    
asked by anonymous 28.08.2017 / 16:06

1 answer

3

The problem here is that route systems choose the first compatible route to construct the route rather than the custom one.

To specify a custom route, use the Html.BeginRouteForm() . In it you pass the name of the route you created, in this case it is Search_route .

Example:

@using (Html.BeginRouteForm("Search_route", FormMethod.Get, new { @class = "search-form" }))
    
04.09.2017 / 20:58