Wednesday, 23 April 2014

Pagging in MVC




Pagging in MVC

View Side:
At top of the view:

@model PagedList.IPagedList<Properties>

@Html.Partial("_Pager")


_Pager.cshtml:

<div class="pagging">
    <div class="left">
        Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber)
        of @Model.PageCount
    </div>
    <div class="right">
        @{var action = ViewContext.RouteData.GetRequiredString("action");}
        @if (Model.HasPreviousPage)
        {
            @Html.ActionLink("First", action, new { page = 1,CategoryId =  ViewBag.CategoryId , sortBy = ViewBag.sb, IsLoggedIn = ViewBag.ls, sortOrder = ViewBag.so, Name = ViewBag.Name, Email = ViewBag.mail, Gender = ViewBag.gen, AgeFrom = ViewBag.af, AgeTo = ViewBag.at, CountryLocated = ViewBag.col, CityLocated = ViewBag.cil, IsActive = ViewBag.act, UserImage = ViewBag.pho, CountryCode = ViewBag.cc });
            @Html.Raw(" ");
            @Html.ActionLink("Prev", action, new { page = Model.PageNumber - 1,CategoryId =  ViewBag.CategoryId , sortBy = ViewBag.sb, IsLoggedIn = ViewBag.ls, sortOrder = ViewBag.so, Name = ViewBag.Name, Email = ViewBag.mail, Gender = ViewBag.gen, AgeFrom = ViewBag.af, AgeTo = ViewBag.at, CountryLocated = ViewBag.col, CityLocated = ViewBag.cil, IsActive = ViewBag.act, UserImage = ViewBag.pho, CountryCode = ViewBag.cc });
        }
        else
        {
            <span>First</span>
            @Html.Raw(" ");
            <span>Prev</span>
        }
        @if (Model.HasNextPage)
        {
            @Html.ActionLink("Next", action, new { page = Model.PageNumber + 1,CategoryId =  ViewBag.CategoryId , sortBy = ViewBag.sb, IsLoggedIn = ViewBag.ls, sortOrder = ViewBag.so, Name = ViewBag.Name, Email = ViewBag.mail, Gender = ViewBag.gen, AgeFrom = ViewBag.af, AgeTo = ViewBag.at, CountryLocated = ViewBag.col, CityLocated = ViewBag.cil, IsActive = ViewBag.act, UserImage = ViewBag.pho, CountryCode = ViewBag.cc });
            @Html.Raw(" ");
            @Html.ActionLink("Last", action, new { page = Model.PageCount,CategoryId =  ViewBag.CategoryId , sortBy = ViewBag.sb, IsLoggedIn = ViewBag.ls, sortOrder = ViewBag.so, Name = ViewBag.Name, Email = ViewBag.mail, Gender = ViewBag.gen, AgeFrom = ViewBag.af, AgeTo = ViewBag.at, CountryLocated = ViewBag.col, CityLocated = ViewBag.cil, IsActive = ViewBag.act, UserImage = ViewBag.pho, CountryCode = ViewBag.cc });
        }
        else
        {
            <span>Next</span>
            @Html.Raw(" ");
            <span>Last</span>
        }
    </div>
</div>

Controller Side:

var users = new PostRepository().GetUserPictures(objPro_PictureMaster);
            const int pageSize = 10;
            var pageNumber = (objPro_PictureMaster.page ?? 1);
            ViewBag.TotalUsers = users.Count();
            var enumerable = users as IList<Pro_PostMaster> ?? users.ToList();
            ViewBag.FilteredCount = enumerable.Count();
            ViewBag.CategoryDropDownList = categoryBind(CategoryId);
            return View(enumerable.ToPagedList(pageNumber, pageSize));

1 comment: