2012-11-17 36 views
1

我想要在一個控制器的索引搜索,以獲取在同一視圖(指數)的搜索結果呈現, 的,這是行動:在MVC 4搜尋和同樣的觀點

public ActionResult Index(string SeatchParam) 
{ 
var Employees = db.Employees.Include(e => e.Location).Include(e =>   e.Department).Where(e => e.FirstName.Contains(SeatchParam)|| e.FatherName.Contains(SeatchParam) || e.LastName.Contains(SeatchParam) || e.Location.LocationName.Contains(SeatchParam) || e.Department.DepartmentName.Contains(SeatchParam)).Take(10); 
     return View(Employees.ToList()); 
    } 

和看法是這樣的:

@model IEnumerable<OG.Models.Employee> 

@{ 
ViewBag.Title = "Index"; 
} 

<h2>Search Employees</h2> 

<form action="Employee/Index" method="get"> 
<input type="text" name="SearchParam" /> 
<input type="submit" value="Search" /> 


<p> 
@Html.ActionLink("Create New", "Create") 
</p> 


<table border="10"> 
<tr> 

    <th> 
     @Html.DisplayNameFor(model => model.EmployeeId) 
    </th> 

    <th> 
     @Html.DisplayNameFor(model => model.FullName) 
    </th> 

    <th> 
     @Html.DisplayNameFor(model => model.Department.DepartmentName) 
    </th> 

    <th> 
     @Html.DisplayNameFor(model => model.Location.LocationName) 
    </th> 

    <th></th> 
</tr> 

@foreach (var item in Model) { 
<tr> 
    <td> 
     @Html.DisplayFor(modelItem => item.EmployeeId) 
    </td> 
    <td> 
     @Html.DisplayFor(modelItem => item.FullName) 
    </td> 
    <td> 
     @Html.DisplayFor(modelItem => item.Department.DepartmentName) 
    </td> 
    <td> 
     @Html.DisplayFor(modelItem => item.Location.LocationName) 
    </td> 
    <td> 
     @Html.ActionLink("Edit", "Edit", new { id=item.EmployeeId }) | 
     @Html.ActionLink("Details", "Details", new { id = item.EmployeeId }) | 
     @Html.ActionLink("Delete", "Delete", new { id = item.EmployeeId }) 
    </td> 
</tr> 
} 

</table> 
</form> 

當我搜索,我發現沒有結果,我可以做,沒有AJAX或JSON ??? 謝謝,

+1

您粘貼控制器代碼兩次,但不是你的視圖代碼。你能否發表你的觀點?你確定數據庫中有與搜索參數匹配的數據嗎?你有調試你的行動,你有正確的'SeatchParam'? – nemesv

+0

謝謝我編輯它。 –

+0

難道是因爲你拼寫的動作參數與輸入不同嗎? '字符串SeatchParam'>'name =「SearchParam」' –

回答

2

您通過SeatchParam到您的控制器,而您的輸入被稱爲SearchParam

所以更改

public ActionResult Index(string SearchParam)