在MVC 4中搜索并在相同视图中渲染

时间:2012-11-17 18:30:25

标签: asp.net-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 个答案:

答案 0 :(得分:2)

在您的输入被调用SeatchParam

时,您将SearchParam传递到控制器中

所以改变

public ActionResult Index(string SearchParam)