Search code examples
asp.net-mvc-4asp.net-mvc-viewmodelpagedlist

PagedList.IPaged<ProjectName.WebUI.Model>does not contain a definition for Profiles(are you missing ising directive or refrence


I am a new in Asp.net and i have one error in my view i realised repository pattern in my project with Pagedlist and ViewModel

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HoboAnimal.Domain.Entities
{
    public class Profile
    {

        public int ProfileID { get; set; }
        public string Color { get; set; }
        public string Image { get; set; }
        public string PlaceHolder { get; set; }
        public DateTime? DateOfPubl { get; set; }
        public string CurrentCategory { get; set; }

            }
}

My ViewModel class and controller:

using System.Collections.Generic;
using HoboAnimal.Domain.Entities;
using PagedList.Mvc;
using PagedList;

namespace HoboAnimal.WebUI.Models
{
    public class ProfilesListViewModel
    {

        public PagedList.IPagedList<Profile> Profiles {get; set;}

    }
}

And the view:

@model PagedList.IPagedList<HoboAnimal.WebUI.Models.ProfilesListViewModel>
    @using PagedList.Mvc;
    @using HoboAnimal.WebUI.Models


    <link href="~/Content/PagedList.css" rel="stylesheet" type="text/css" />
    @{
        ViewBag.Title = "Profiles";
    }


    @foreach (var p in Model.Profiles)
    {


        <h2>@p.Image</h2>
        <h3>@p.DateOfPubl</h3>
        <h2>@p.CurrentCategory</h2>

    }

Страница @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) из @Model.PageCount

@Html.PagedListPager(Model, page => Url.Action("Index", new { page }))

I have the error in this lane:

@foreach (var p in Model.Profiles)

PagedList.IPagedList' does not contain a definition for 'Profiles' and no extension method 'Profiles' accepting a first argument of type 'PagedList.IPagedList' could be found (are you missing a using directive or an assembly reference?)

I have no idea how to fix it, i tried to add @using directive in my model but it not helps Thank U


Solution

  • PagedList should be implemented somewhere similar to this. No need to create a view model for a PagedList.

    Controller

    using PagedList;
    
    public ViewResult Index(int? page)
    {
       var profiles = db.Profiles.ToList();
    
       int pageSize = 3;
       int pageNumber = (page ?? 1);
    
       return View(profiles.ToPagedList(pageNumber, pageSize));
    }
    

    View

    @model PagedList.IPagedList<HoboAnimal.WebUI.Models.ProfilesListViewModel>
    @using PagedList.Mvc;
    @using HoboAnimal.WebUI.Models
    
    <link href="~/Content/PagedList.css" rel="stylesheet" type="text/css" />
    @{
        ViewBag.Title = "Profiles";
    }
    
    @foreach (var p in Model)
    {
        <h2>@p.Image</h2>
        <h3>@p.DateOfPubl</h3>
        <h2>@p.CurrentCategory</h2>
    }
    
    <br />
    Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount
    
    @Html.PagedListPager(Model, page => Url.Action("Index", 
        new { page }))