我的问题是在我的MVC项目中实现动态搜索。到目前为止,我只实现了分页。我希望搜索返回在数据库中找到的满足输入的所有结果,而不仅仅是当前页面上的结果。为此,我有红色,我需要使用Angular JS并做了一些研究和一些尝试但没有工作。 附:由于搜索将在我的索引页面上,我认为最好将Index ActionResult缓存,以便每次在搜索框中输入符号时都不调用此方法?你有什么看法。
如何在列上创建动态搜索和排序? 这是我的代码:
控制器
public class TeachersController : Controller
{
private RSEntities db = new RSEntities();
// GET: Teachers
// Use cache only if implemented dynamic search, otherwise not needed
//[OutputCache(Duration =110, Location=System.Web.UI.OutputCacheLocation.Any, NoStore = true)]
public ActionResult Index(int page = 1, int pageSize = 8)
{
List<tblTeacher> listTeachers = db.tblTeachers.ToList();
PagedList<tblTeacher> model = new PagedList<tblTeacher>(listTeachers, page, pageSize);
//return View(db.tblTeachers.ToList());
return View(model);
}
}
这是HTML视图
@*@model IEnumerable<Rating_System.Models.tblTeacher>*@
@using PagedList.Mvc;
@model PagedList.IPagedList<Rating_System.Models.tblTeacher>
@{
ViewBag.Title = "Списък с преподаватели";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Преподаватели</h2>
<p>
@Html.ActionLink("Добавяне на запис", "Create")
</p>
<label>Quick Search: <input id="search" type="text"></label>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.First().FirstName)
</th>
<th>
@Html.DisplayNameFor(model => model.First().SecondName)
</th>
<th>
@Html.DisplayNameFor(model => model.First().Title)
</th>
<th>
@Html.DisplayNameFor(model => model.First().PersonalCabinet)
</th>
<th>
@Html.DisplayNameFor(model => model.First().TelNum)
</th>
<th>
@Html.DisplayNameFor(model => model.First().Email)
</th>
<th>
@Html.DisplayNameFor(model => model.First().TDepartment)
</th>
<th></th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.FirstName)
</td>
<td>
@Html.DisplayFor(modelItem => item.SecondName)
</td>
<td>
@Html.DisplayFor(modelItem => item.Title)
</td>
<td>
@Html.DisplayFor(modelItem => item.PersonalCabinet)
</td>
<td>
@Html.DisplayFor(modelItem => item.TelNum)
</td>
<td>
@Html.DisplayFor(modelItem => item.Email)
</td>
<td>
@Html.DisplayFor(modelItem => item.TDepartment)
</td>
<td>
@Html.ActionLink("Редактирай", "Edit", new { id = item.ID }) |
@Html.ActionLink("Детайли", "Details", new { id = item.ID }) |
@Html.ActionLink("Изтрий", "Delete", new { id = item.ID })
</td>
</tr>
}
</table>
<br />
@Html.PagedListPager(Model, page => Url.Action("Index", new { page, pageSize = Model.PageSize }))
Резултати @Model.FirstItemOnPage - @Model.LastItemOnPage от общо @Model.TotalItemCount преподаватели