2

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);
    }
Community
  • 1
  • 1
MyDaftQuestions
  • 4,487
  • 17
  • 63
  • 120

1 Answers1

0

The problem is your AllRefineSearch route with its default values: "Products" controller, "Connectors" action and "0" startIndex.

Providing default values in a route also means that those segments are optional. In your case the url "/" will match that route, and each segment will take its default value.

A similar process is followed when generating a Url for a link,form, etc. You are providing "Products" as controller and "Connectors" as action, so the AllRefineSearch will be used. Because they are the default values, it will simplify the url for the form tag as "/". Finally on submit the inuput values are added in the query string.

Try without providing default values in the search route for the controller and action segments:

routes.MapRoute(
    name: "AllRefineSearch",
    url: "{controller}/{action}/{startIndex}/",
    defaults: new { startIndex = 0, pageSize = 10 }
);
Daniel J.G.
  • 34,266
  • 9
  • 112
  • 112