搜索表单提交后,URL中没有搜索参数

时间:2012-11-21 18:04:27

标签: c# asp.net-mvc asp.net-mvc-3 razor asp.net-mvc-routing

我有一个这样的搜索表单:

<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>

调用“Search”操作方法:

public ActionResult Search(string city, string q)
        {
            ...
            return View(model);
        }

在这里,我收到两个值,搜索也很好。 我的浏览器中的网址是:

http://localhost/mysite/item/Search?city=london

正如您所看到的,我在URL中缺少“q”参数 我在这做错了什么?

4 个答案:

答案 0 :(得分:1)

搜索字段的输入类型必须是文本,而不是搜索。

答案 1 :(得分:1)

您的表单方法是POST,因此不会通过查询字符串发送值。将POST更改为GET,您应该看到它们。

答案 2 :(得分:1)

尝试关闭代码<input ... />

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

答案 3 :(得分:0)

你可以效仿我的例子:

型号:

public class SearchModel{
  public String City { get; set; }
  public String Q { get; set; }
}

查看:

@model SearchModel
@using (@Html.BeginForm("Search", "Items", FormMethod.Post, new {@id = "Form"})) {
   @Html.HiddenFor(m => m.City)
   @Html.HiddenFor(m => m.Q)
}

控制器:

[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);
}