【问题标题】:After search form submit there is no search parameter in URL搜索表单提交后,URL 中没有搜索参数
【发布时间】:2012-11-21 18:04:26
【问题描述】:

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

<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”参数。
我在这里做错了什么?

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

    • 这并不能解决为什么 london 仍然通过 GET 参数发送...
    • 搜索表单所在页面的 URL 是什么?也许它来自那里?
    【解决方案2】:

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

    【讨论】:

    • 我现在把它改成'text'了,还是一样。
    【解决方案3】:

    尝试关闭标签&lt;input ... /&gt;

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

    【讨论】:

      【解决方案4】:

      你可以按照我的例子:

      型号:

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

      【讨论】:

      • 你需要区分 HttpPostHttpGet :)
      猜你喜欢
      • 1970-01-01
      • 2014-08-18
      • 1970-01-01
      • 2017-03-08
      • 1970-01-01
      • 2018-10-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多