My MVC4 website shows items from a database The user can 'refine' their search from within a web form. After this, they click the search button and their results are shown.
At this stage, I only have 1 route
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/",
defaults: new { controller = "Home", action = "Index" }
);
When I load the page for the first time, the address is www.mydomain.com/products/connectors/
, after I make a search it appends my querystring
www.mydomain.com/products/connectors/?Gender=1
Now, I'm adding pagination and would like the user to be able to select Next page. I've used Marc's answer from How do I do pagination in ASP.NET MVC? to do this. Pagination works great.
This is the routeconfig.cs
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "AllRefineSearch",
url: "{controller}/{action}/{startIndex}/",
defaults: new { controller = "Products", action = "Connectors", startIndex = 0, pageSize = 10 }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/",
defaults: new { controller = "Home", action = "Index" }
);
}
}
The issue though is now when I click the search button, my Controller and Action are removed from the address. In other words, the address is www.mydomain.com/?Gender=1
I don't know how to keep the controller and action in the URL as I thought the route was specifying this!
My form is
@using (Html.BeginForm("Connectors", "Products", FormMethod.Get))
{
@Html.DropDownListFor(a => a.Gender, Model.ConnectorRefineSearch.Gender, "---Select---")
<input type="submit" value="Search" class="searchButton" />
}
And my controller
[HttpGet]
public ActionResult Connectors(ConnectorVm connector, int startIndex, int pageSize)
{
connector.UpdateSearch();
return View(connector);
}