I have a problem with search box. So here is the code to generate the form.
@using (@Html.BeginForm("Index", "Search"))
{
<input name="query" type="text" />
<input type="submit" />
}
and the route:
routes.MapRoute(
"Search",
"Search",
new { controller = "Search", action = "Index"}
);
So basically right now when user types in something in search box and clicks the button, it takes him to url /Search and displays the search value. But I would like to have /Search?query=searchedvaluehere. Also when I manually type in /Search?query=something it does search for something. So all I need is to have the query in the URL to appear automatically.
The Controller action looks like this:
[HttpGet]
public ActionResult Index(string query)
{
//some code here
}
You just need to set the form action to
get, instead ofpost, which is the default. There’s an overload ofHtml.BeginForm()which lets you set that.