您可以將您的輸入框保留在表單中,並將表單的方法值保持爲GET。
@using (Html.BeginForm("Search", "Students", FormMethod.Get, null))
{
<input type="text" name="searchTerm" />
<input type="submit" />
}
現在你應該有一個名爲Search
您StudentsController
內的操作方法,它接受一個名爲searchTerm
參數字符串值。
public ActionResult Search(string searchTerm)
{
// use searchTerm variable to get data and pass to view
return View();
}
現在,在您的視圖(~/Views/Students/Search.cshtml
),可以使用通過數據來顯示搜索結果。
對於分頁,您可以在您的操作方法中添加另一個參數,您將使用該參數來獲取特定的數據子集。
public ActionResult Search(string searchTerm,int page=1,size=10)
{
// use searchTerm variable to get data and pass to view
// page number is in page variable
// size is in size variable
return View();
}
現在,您需要調整您的視角來顯示所有的頁碼和鏈接到相同的操作方法,並通過頁面數量網址像
/Students/Search?searchTerm=Java&page=2&size=25
謝謝了很多,非常有益和明確。@ Shyju –
它的工作,但分頁不起作用,也許我錯過了一些東西,你可以向我解釋更多關於分頁和感謝 –
你基本上需要編寫的代碼,以大小的項目總數除以並將其發送到視圖與您的數據一起(顯示)並顯示它。 – Shyju