Reputation: 6839
I have a search form like this:
<form action="@Url.Action("Search", "Items", null, null)" method="POST">
<input type="search" placeholder="Search" name="q" value="some search term">
<input type="hidden" name="city" value="london" />
</form>
This invoke "Search
" action method:
public ActionResult Search(string city, string q)
{
...
return View(model);
}
Here I receive both values and search gone fine. But URL in my browser is:
http://localhost/mysite/item/Search?city=london
as you can see I am missing "q" parameter in URL.
What have I done wrong here?
Upvotes: 0
Views: 299
Reputation: 677
You can follow my example:
Model:
public class SearchModel{
public String City { get; set; }
public String Q { get; set; }
}
View:
@model SearchModel
@using (@Html.BeginForm("Search", "Items", FormMethod.Post, new {@id = "Form"})) {
@Html.HiddenFor(m => m.City)
@Html.HiddenFor(m => m.Q)
}
Controller:
[HttpGet]
public ActionResult Search(string city, string q)
{
var model = new SearchModel {
City = "london",
Q = "some search term"
};
return View(model);
}
[HttpPost]
public ActionResult Search(SearchModel model)
{
//.....
return View(model);
}
Upvotes: 0
Reputation: 26386
Try to close the tag <input ... />
<input type="text" placeholder="Search" name="q" value="some search term" />
Upvotes: 1
Reputation: 4310
You form method is POST, so values are not sent via the query string. Change the POST to GET and you should see them.
Upvotes: 1