我對MVC相當陌生。我正在設置一個搜索頁面來搜索數據庫並返回結果。搜索框在我看來是一個Html.BeginForm內,看起來像這樣:ActionResult不會被routelink調用。表單收集的罪魁禍首?
<% using (Html.BeginForm())
{ %>
<%= Html.TextBox("searchBox", null, new { @id = "searchBox" })%>
<div id="searchButtonsDiv">
<input type="submit" value="Search" />
</div>
<% } %>
//Results are returned in a ul and orgainized
//Pagination below
<% if (Model.HasPreviousPage)
{ %>
<%= Html.RouteLink("Previous", "SearchResults", new { page = (Model.PageIndex - 1) })%>
<% } %>
<% if (Model.HasNextPage)
{ %>
<%= Html.RouteLink("Next", "SearchResults", new { formCollection = "", page = (Model.PageIndex + 1) })%>
<% } %>
我使用的FormCollection傳遞給我的控制器看起來像這樣:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Index(FormCollection formCollection, int? page)
{
var searchString = formCollection["searchBox"];
var results = resultsRepository.GetResults();
var paginatedResults = new PaginatedList<Driver>(results, page ?? 0, pageSize);
return View(paginatedResults);
}
到目前爲止好。當我輸入一個單詞並按下提交按鈕時,索引被調用並且數據庫相應地返回。 ul得到結果填充,並且當pageSize結果多於10(在我的情況下)時,Next鏈接顯示出來。
當我點擊「下一步」時,只會加載默認頁面。沒有分頁或類似的東西。我很確定它與我的Index ActionResult具有FormCollection作爲參數的事實有關。我以爲我讀過的地方只能處理字符串/整數?這裏是MapRoute:
routes.MapRoute(
"SearchResults",
"Drivers/Index/{formCollection}/{page}",
new { controller = "Drivers", action = "Index", formCollection = "", page = "" }
);
我完全錯過了什麼,或者有沒有辦法處理這個?我知道我可以使用jquery/ajax發送包含在搜索列表框中的字符串,但我不想這樣做,因爲後來我計劃添加複選框作爲過濾搜索的手段等。
我試過幾個不同的方式來設置formCollection的值,包括創建一個新的FormCollection,添加searchBox,以及傳遞字符串等。
當你點擊Next時,它只是從Index actionresult調用一個HttpGet? – 2010-02-24 22:45:10
它調用我沒有參數的其他ActionResult索引。 (公共ActionResult指數(){ 返回查看(); 我認爲它永遠不會被調用,因爲它的FormCollection不能用於routelink。我認爲它必須是一個字符串或int。 } – Darcy 2010-02-25 13:48:20