How to get Html.BeginForm to GET to fix MVC route
In my ASP.NET MVC application, I have the following GET input field:
<% using (Html.BeginForm("Search", "Products", FormMethod.Get) { %>
<input type="text" name="searchQuery" id="searchQuery" />
<% } %
code>
I want this to move to a route:
routes.MapRoute("ProductSearchRoute",
"Products/Search/{searchQuery}/{pageNumber}",
new { controller = "Products", action = "Search", pageNumber = 1 });
code>
The problem is that it goes to / Products as a query string, eg. Products? SearchQuery = motor. How can I use it in ProductSearchRoute and shape / Products / Search / Motoroil instead?
+2
source to share
4 answers
As @Daniel Elliott suggested, use a BeginRouteForm. In order for your URL to be generated correctly, you must set the route values ββwith the same name defined in the route table.
@using (Html.BeginRouteForm("ProductSearchRoute", new { searchQuery= "my query", pageNumber = 1 })
{
}
0
source to share