0

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

<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

4 回答 4

1

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

于 2012-11-21T18:07:34.973 回答
1

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

于 2012-11-21T18:21:50.103 回答
1

尝试关闭标签<input ... />

<input type="text" placeholder="Search" name="q" value="some search term" />
于 2012-11-21T18:22:03.747 回答
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);
}
于 2013-05-24T08:09:04.750 回答