0

I am trying to add pagination to my view and edited my controller to create it, but when I try to include the PagedList model I get an error as I can't include my original model. This is what my view looks like right now:

@model IEnumerable<metric_tool.Models.Metric>
@model PagedList.IPagedList<metric_tool.Models.Metric>
@using PagedList
@{
ViewBag.Title = "Metrics List";
}

<h2>Metrics</h2>

<p>
    @Html.ActionLink("Create New", "Create")

    @using (Html.BeginForm("Index", "Metric", FormMethod.Get))
    {
    <p>
        Category: @Html.DropDownList("metricCategory","All")
        Client: @Html.DropDownList("metricClient","All")
        Phase: @Html.DropDownList("metricPhase","All")
        Hierarchy: @Html.DropDownList("metricHierarchy", "All")
        Thresholds: @Html.DropDownList("metricThreshold", "All") <br />
    </p>
    <p>
        Data Source Search: @Html.TextBox("searchDataSrc") <input type="submit" value="Search" /> 
        Description Search: @Html.TextBox("SearchString", ViewBag.CurrentFilter as string) <input type="submit" value="Search" />
    </p> 
    }

</p>
<table class="table">
    <tr>
        <th>
            @Html.ActionLink("Metric","Index", new { sortOrder = ViewBag.MetricSort, currentFilter=ViewBag.CurrentFilter })
        </th>
        <th>
            @Html.ActionLink("Category", "Index", new { sortOrder = ViewBag.CategorySort, currentFilter = ViewBag.CurrentFilter })
        </th>
        <th>
            @Html.DisplayNameFor(model => model.SelectClient)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.SelectPhase)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.SelectHierarchy)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.SelectDataSource)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.SelectThresholds)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Description)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    .....

</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 }))

I was looking at using a dynamic model, but wasn't able to get it to work instead.

rontho1992
  • 116
  • 1
  • 11
  • 1
    Why do you need both , especially this one: `@model IEnumerable` This one should be enough ` @model PagedList.IPagedList` – IndieTech Solutions Apr 30 '15 at 21:38
  • In the event that I need multiple models, I generally build a ViewModel with the Models that I need. Also, if it works for you, take a look at some plugins that handle pagination, etc. https://www.datatables.net/ – Papa Burgundy Apr 30 '15 at 22:15
  • Wrap your two models into one using ViewModel. I want to show you the code snippet but don't know why i can answer this question – Agung Setiawan May 01 '15 at 03:30

1 Answers1

0

I actually do it this way. I have PagedViewModel.cs:

using System.Collections.Generic;

namespace App.Web.ViewModels
{
    public class PagedViewModel<T>
    {
        public int      PageNumber  { get; set; }
        public int      PageSize    { get; set; }

        public long     ItemCount   { get; set; }
        public int      PageCount   { get; set; }

        public bool     HasPrevPage { get; set; }
        public bool     HasNextPage { get; set; }

        public List<T>  Items       { get; set; }

        public PagedViewModel()
        {
            Items = new List<T>();
        }
    }

}

I have PagedResult:

using System;
using System.Collections.Generic;

namespace App.Core.Infrastructure.Common.Paging
{
    public class PagedResult<T>
    {
        public int      PageNumber  { get; set; }
        public int      PageSize    { get; set; }

        public long     ItemCount   { get; set; }
        public int      PageCount   { get; set; }

        public bool     HasPrevPage { get; set; }
        public bool     HasNextPage { get; set; }

        public List<T>  Items       { get; set; }

        public PagedResult(int pageNumber, int pageSize, long totalCount, IEnumerable<T> source)
        {
            Items = new List<T>();
            Items.AddRange(source);

            PageNumber = pageNumber;
            PageSize = pageSize;

            ItemCount = totalCount;
            PageCount = (int)Math.Ceiling(totalCount / (double)pageSize);

            HasPrevPage = PageNumber > 1;
            HasNextPage = PageNumber < PageCount;
        }
    }

}

And I have base class for my services:

using App.Core.Infrastructure.Common.Paging;
using App.Core.Models;
using System;
using System.Linq;

namespace App.Core.Infrastructure.Services
{
    public abstract class BaseService
    {
        protected PagedResult<T> GetPagedResult<T>(IQueryable<T> repository, int pageNumber, int pageSize)
            where T : BaseEntity
        {
            pageNumber = Math.Max(1, pageNumber);

            var total = repository.Count();
            var items = repository
                .Skip((pageNumber - 1) * pageSize)
                .Take(pageSize)
                .ToList();

            return new PagedResult<T>(pageNumber, pageSize, total, items);
        }
    }
}

Sample service:

public class PlanService : BaseService
{
    public PagedResult<Plan> GetPlans(int pageNumber, int pageSize)
    {
        return GetPagedResult(_database.Plans.OrderBy(x => x.Id), pageNumber, pageSize);
    }
}

And I use Automapper to convert PagedResult to PagedViewModel:

        Mapper.CreateMap<Plan, PlanViewModel>()
            .ForMember(x => x.PlanTypeValue, o => o.MapFrom(s => s.PlanType))
            .ForMember(x => x.PlanTypeText, o => o.MapFrom(s => s.PlanType.ToString()));
        Mapper.CreateMap<PagedResult<Plan>, PagedViewModel<PlanViewModel>>();

In the view you can just specify @model PagedViewModel<PlanViewModel>. Sounds like a bit complicated. But it works pretty well.

Roman Pushkin
  • 5,639
  • 3
  • 40
  • 58