0
我有一個基本的MVC應用程序,我試圖實現一些搜索功能。當我輸入數據並點擊「搜索」時,url會按預期更新,但索引頁面仍會返回所有結果,而不是過濾結果。我有我的代碼在下面,並想知道是否有人能夠指出爲什麼會是這種情況?根據我過去的做法,我不相信我需要一個單獨的Post方法,但我可能是錯的,因爲我還是新手。謝謝。ASP.NET MVC搜索框不發佈返回索引
INDEX.CSHTML:
<p>
@Html.ActionLink("Create New Employee", "Create")
</p>
<form asp-action="Index" method="get">
<div class="form-actions no-color">
<p>
Find by name: <input type="text" name="searchString" value="@ViewBag.CurrentFilter" />
<input type="submit" value="Search" class="btn btn-default" /> |
<a asp-action="Index">Back to List</a>
</p>
</div>
</form>
控制器:返回
public ActionResult Index(string searchString)
{
var person = from p in db.Person
select p;
ViewBag.CurrentFilter = searchString;
if (!String.IsNullOrEmpty(searchString))
{
person = person.Where(s => s.LastName.Contains(searchString));
//|| p.FirstName.Contains(searchString));
}
return View(db.Person.ToList());
}
URL點擊搜索時:
http://localhost:9999/Person/Index?searchString=smith
工作就像一個魅力,謝謝! – AndrewC10