I was working on a very simple ASP.NET MVC project. I was looking for ways to make the filtering, sorting, and paging worked.
I found a simple solution. I tried it and it worked fine. Here's the code:
Controller:
using PagedList;
private MyEntity ctx = new MyEntity();
public ActionResult Index(string sortOrder, string inSearch, string searchString, int? page)
{
ViewBag.CurrentSort = sortOrder;
ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "Name desc" : "";
ViewBag.DateSortParm = sortOrder == "DateHired" ? "DateHired desc" : "DateHired";
if (Request.HttpMethod == "GET")
{
searchString = inSearch;
}
else
{
page = 1;
}
ViewBag.CurrentFilter = searchString;
var employees = from x in ctx.Employees
select x;
if (!String.IsNullOrEmpty(searchString))
{
employees = employees.Where(x => x.Name.ToUpper().Contains(searchString.ToUpper()));
}
switch (sortOrder)
{
case "Name desc":
employees = employees.OrderByDescending(x => x.Name);
break;
case "Address":
employees = employees.OrderBy(x => x.Address);
break;
case "Age desc":
employees = employees.OrderByDescending(x => x.Age);
break;
case "Salary desc":
employees = employees.OrderByDescending(x => x.Salary);
break;
case "DateHired desc":
employees = employees.OrderByDescending(x => x.DateHired);
break;
default:
employees = employees.OrderBy(x => x.Name);
break;
}
int pageSize = 2;
int pageNumber = (page ?? 1);
return View(employees.ToPagedList(pageNumber, pageSize));
}
Views:
@model PagedList.IPagedList<MVCNIGHT.Infrastructure.Employee>
@{
ViewBag.Title = "Index";
}
<h2>Employee List</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
@using (Html.BeginForm())
{
<p>
Search by Name: @Html.TextBox("SearchString", ViewBag.CurrentFilter as string)
<input type="submit" value="Search" /></p>
}
<table>
<tr>
<th></th>
<th>
@Html.ActionLink("Name", "Index", new { sortOrder=ViewBag.NameSortParm, currentFilter=ViewBag.CurrentFilter })
</th>
<th>
@Html.ActionLink("Address", "Index", new { sortOrder=ViewBag.NameSortParm, currentFilter=ViewBag.CurrentFilter })
</th>
<th>
@Html.ActionLink("Age", "Index", new { sortOrder = ViewBag.DateSortParm, currentFilter = ViewBag.CurrentFilter })
</th>
<th>
@Html.ActionLink("Salary", "Index", new { sortOrder = ViewBag.DateSortParm, currentFilter = ViewBag.CurrentFilter })
</th>
<th>
@Html.ActionLink("Date Hired", "Index", new { sortOrder = ViewBag.DateSortParm, currentFilter = ViewBag.CurrentFilter })
</th>
</tr>
@foreach (var item in Model) {
<tr>
<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>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Address)
</td>
<td>
@Html.DisplayFor(modelItem => item.Age)
</td>
<td>
@Html.DisplayFor(modelItem => item.Salary)
</td>
<td>
@Html.DisplayFor(modelItem => item.DateHired)
</td>
</tr>
}
</table>
<div>
Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber)
of @Model.PageCount
@if (Model.HasPreviousPage)
{
@Html.ActionLink("<<", "Index", new { page = 1, sortOrder = ViewBag.CurrentSort, currentFilter=ViewBag.CurrentFilter })
@Html.Raw(" ");
@Html.ActionLink("< Prev", "Index", new { page = Model.PageNumber - 1, sortOrder = ViewBag.CurrentSort, currentFilter=ViewBag.CurrentFilter })
}
else
{
@:<<
@Html.Raw(" ");
@:< Prev
}
@if (Model.HasNextPage)
{
@Html.ActionLink("Next >", "Index", new { page = Model.PageNumber + 1, sortOrder = ViewBag.CurrentSort, currentFilter=ViewBag.CurrentFilter })
@Html.Raw(" ");
@Html.ActionLink(">>", "Index", new { page = Model.PageCount, sortOrder = ViewBag.CurrentSort, currentFilter=ViewBag.CurrentFilter })
}
else
{
@:Next >
@Html.Raw(" ")
@:>>
}
</div>
Note: This is a simple solution. It works. It's okay for a really small project. But as professional developer, I don't recommend to use it in production. Instead, use the above sample for learning purposes only.
No comments:
Post a Comment