2012-11-21 44 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