首页 文章

PagedList.IPaged <ProjectName.WebUI.Model>不包含Profiles的定义(您是否缺少ising指令或参考

提问于
浏览
1

我是Asp.net的新手,我在视图中有一个错误,我在使用Pagedlist和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; }

            }
}

我的ViewModel类和控制器:

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;}

    }
}

并且观点:

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

我在这条道路上有错误:

@foreach (var p in Model.Profiles)

PagedList.IPagedList'不包含'Profiles'的定义,也没有扩展方法'Profiles'接受类型'PagedList.IPagedList'的第一个参数'(你是否缺少using指令或汇编引用?)

我不知道如何解决它,我试图在我的模型中添加@using指令,但它没有帮助谢谢你

1 回答

  • 1

    应该在类似于此的地方实现PagedList . 无需为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>
    }
    
    
    Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount @Html.PagedListPager(Model, page => Url.Action("Index", new { page }))

相关问题