2012-11-21 39 views
0

我有这样的一个搜索表单:搜索表单提交后出现在URL没有搜索参数

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

回答

1

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

+0

这并不能解决为什么'london'仍然通过GET参数发送... –

+0

搜索表单所在页面的URL是什么?也许它来自那里? –

1

您搜索字段的输入类型需要为文本,而不是搜索。

+0

我已将它更改为'文本',现在仍然相同。 – 1110

1

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

<input type="text" placeholder="Search" name="q" value="some search term" /> 
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); 
} 
+0

您需要区分** HttpPost **和** HttpGet ** :) – LazyCatIT