1

How can i set mapRoute for search results page? My code doesn't work.

Global.asax.cs

 routes.MapRoute(
           name: "SearchResults",
           url: "{action}/{Keyword}",
           defaults: new { controller = "Home", action = "Search" }
       );

Search Form

@using (Html.BeginForm("Search", "Home", FormMethod.Get))
            {
                @Html.TextBox("Keyword",null , new { @class = "SearchBox" })
                <input type="submit" value="Search" />
            }

HomeController.cs

public ActionResult Search(string Keyword)
    {
        GamesContext db = new GamesContext();
        var SearchResults= (from i in db.Games where i.GameName.Contains(Keyword) || i.GameDesc.Contains(Keyword) select i).Take(20).ToList();

        return View(SearchResults.AsEnumerable());
    }
sasailic
  • 15
  • 4

1 Answers1

0

This one works for me (should be before default route):

routes.MapRoute(
     "SearchResults",
     "Search/{Keyword}",
     new { controller = "Search", action = "SearchAction" }
);

Creating an ActionLink and MapRoute that there is a constant name in it

And there's a point to use new controller for search instead of home with this route.

Community
  • 1
  • 1
Sergey Shabanov
  • 176
  • 2
  • 11