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

Asked

Viewed 107 times

4

I have a route problem(again), I use the 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
}

When I request my url returns /Home/Search? search_input=Casa&search_input_category=vectors That is, the route is correct, but it is not being applied in the <form> for some reason. It is possible to apply the route to the form in some way?

1 answer

3


The problem there is that the route systems choose the first compatible route to build the route and not the custom one.

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

Example:

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.