Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Search Route in ASP.NET MVC

I have a simple search form in my master page and a serach controller and view. I'm trying to get the following route for the string search term "myterm" (for example): root/search/myterm

The form in the master page :

<% using (Html.BeginForm("SearchResults", "Search", FormMethod.Post, new { id = "search_form" }))
                           { %>
                        <input name="searchTerm" type="text" class="textfield" />
                        <input name="search" type="submit" value="search" class="button" />
                        <%} %>

The Controller Action:

public ActionResult SearchResults(string searchTerm){...}

The Route I'm Using :

routes.MapRoute(
          "Search",
          "search/{term}",
          new { controller = "Search", action = "SearchResults", term = (string)null }
        );

routes.MapRoute(
          "Default",
          "{controller}/{action}",
          new { controller = "Home", action = "Index" }
        );

I'm always getting the url "root/search" without the search term, no matter what search term I enter.

Thanks.

like image 351
vobs Avatar asked Mar 30 '10 22:03

vobs


Video Answer


1 Answers

You're using id in your beginform tag and {term} in your route.

The two need to match.

like image 99
Simon Holman Avatar answered Sep 19 '22 01:09

Simon Holman