假設我有一個帶有文本框的簡單搜索表單。在提交表單時,我將文本框的內容發送給存儲過程,該存儲過程將結果返回給我。我希望結果顯示在表單所在的同一頁面上,除了下面的頁面。在同一頁面顯示搜索表單及其結果的最佳方式?
現在我做以下,但它不工作正是我想要的方式:
「索引」我SearchController觀
@using (Html.BeginForm("SearchResults", "Search", FormMethod.Post, new { @class = "searchform" }))`{
<fieldset>
<legend>Name</legend>
<div class="editor-label">
@Html.Label("Search")
</div>
<div class="editor-field">
@Html.TextBox("Name")
</div>
<input type="submit" value="Search" class="formbutton" />
</fieldset>
@{ Html.RenderPartial("SearchResults", null);
這是我的「SearchResult所」觀點:
@model IEnumerable<MyProject.Models.spSearchName_Result>
<table>
@foreach (var item in Model)
{
<tr>
<td>
@item.Name
</td>
</tr>
}
</table>
這是我的控制器:
// GET: /Search/SearchResult
[HttpPost]
public ActionResult SearchResult(FormCollection collection)
{
var result = myentity.spSearchName(collection["Name"]);
return PartialView("SearchResults", result);
}
我似乎只能將結果顯示在一個全新的頁面上(不是作爲局部視圖嵌入),或者當我加載搜索頁面時出現錯誤,因爲沒有結果(因爲我沒有尚未搜索)。
有沒有更好的方法來實現我想要做的?我覺得我正在反對MVC中的一些最佳實踐。