1110
1110

Reputation: 6839

After search form submit there is no search parameter in URL

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

Answers (4)

Hau Le
Hau Le

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

codingbiz
codingbiz

Reputation: 26386

Try to close the tag <input ... />

<input type="text" placeholder="Search" name="q" value="some search term" />

Upvotes: 1

Michael Dunlap
Michael Dunlap

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

Felix Guo
Felix Guo

Reputation: 2708

Input type for your search field needs to be Text, not Search.

Upvotes: 1

Related Questions