Home > DeveloperSection > Forums > How to Paging with the entity framework is an ASP.NET MVC Application
Anupam Mishra

Total Post:135

Points:949
Posted on    February-05-2016 10:33 PM

 ASP.Net C#  ASP.Net  ASP.NET MVC 
Ratings:


 1 Reply(s)
 301  View(s)
Rate this:
Hi Everyone,

I want to know how to Paging  with the using of entity framework in ASP.NET MVC. I want to display a record in index view with page number .Can anyone give me a solution.

Thank you.



Anupam Mishra

Total Post:135

Points:949
Posted on    February-05-2016 11:25 PM

Finally, I have resolve this problem.
For Solution of above problem, we are creating one student controller and its index view. We want to display  a paging number of records(i.e. First Name,Last Name or Enrollment date) in index view. We are also say, it is complete code of  searching, sorting ,filtering and paging example in an  asp.net mvc 4. For paging we want to download one references i.e. PagedList.

The NuGet PagedList.Mvc package automatically installs the PagedList package as a dependency. The PagedList package installs a PagedList collection type and extension methods for IQueryable and IEnumerable collections. From the Tools menu, select Library Package Manager and then Manage NuGet Packages for Solution.

this is a code of Student Controller Index Action:

 public ViewResult Index(string sortOrder, string currentFilter, string searchString, int? page)
        {
            ViewBag.CurrentSort = sortOrder;
            ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "";
            ViewBag.DateSortParm = sortOrder == "Date" ? "date_desc" : "Date";
            if (searchString != null)
            {
                page = 1;
            }
            else
            {
                searchString = currentFilter;
            }
            ViewBag.CurrentFilter = searchString;
            var students = from s in db.Student
                           select s;
            if (!String.IsNullOrEmpty(searchString))
            {
                students = students.Where(s => s.FirstName.ToUpper().Contains (searchString.ToUpper())
                                       ||s.LastName.ToUpper().Contains(searchString.ToUpper()));
            }
            switch (sortOrder)
            {
                case "name_desc":
                    students = students.OrderByDescending(s => s.LastName);
                    break;
                case "Date":
                    students = students.OrderBy(s => s.EnrollmentDate);
                    break;
                case "date_desc":
                    students = students.OrderByDescending(s => s.EnrollmentDate);
                    break;
                default:  // Name ascending 
                    students = students.OrderBy(s => s.LastName);
                    break;
            }
Now we will change in a student index view. code is as follows:

@model PagedList.IPagedList<StudentUniversity.Models.Student>
@using PagedList.Mvc;
<link href="~/Content/PagedList.css" rel="stylesheet" />
@{
    ViewBag.Title = "Index";
}
    <h2>Index</h2>
<p>
    @Html.ActionLink("Create New",  "Create")
    @using (Html.BeginForm("Index", "Student", FormMethod.Get))
{
    <p>
        Find by name: @Html.TextBox("SearchString", ViewBag.CurrentFilter as string)  
        <input type="submit" value="Search" />
    </p>
}<table>
   <tr>
        <th>
            @Html.ActionLink("First Name", "Index", new { sortOrder = ViewBag.NameSortParm })
        </th>
        <th>Last Name
        </th>
        <th>
            @Html.ActionLink("Enrollment Date", "Index", new { sortOrder = ViewBag.DateSortParm })
        </th>
       <th>Full Name
        </th>
        <th></th>
   
    </tr>
@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.FirstName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.LastName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.EnrollmentDate)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.FullName)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.StudentID }) |
            @Html.ActionLink("Details", "Details", new { id=item.StudentID }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.StudentID })
        </td>
    </tr>
}
</table>
<br/>
Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount
@Html.PagedListPager( Model, page => Url.Action("Index", new { page, sortOrder = ViewBag.CurrentSort, currentFilter=ViewBag.CurrentFilter }) )

Modified On Feb-06-2016 03:41:52 AM

Don't want to miss updates? Please click the below button!

Follow MindStick