【问题标题】:PagedList.IPaged<ProjectName.WebUI.Model>does not contain a definition for Profiles(are you missing ising directive or refrencePagedList.IPaged<ProjectName.WebUI.Model>不包含 Profiles 的定义(您是否缺少 ising 指令或参考
【发布时间】:2026-02-02 07:20:12
【问题描述】:

我是 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' 接受类型的第一个参数 'PagedList.IPagedList' 可以找到(您是否缺少 using 指令或程序集 参考?)

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

【问题讨论】:

  • 你的模型是ProfilesListViewModel的集合,所以它需要是@foreach (var m in Model) { foreach (var p in m.Profiles { .... } }但是你的模型真的是IPagedList&lt;ProfilesListViewModel&gt;还是应该是@model ProfilesListViewModel
  • 它应该是 IPagedList 因为我想使用分页
  • 但是ProfilesProfilesListViewModel 属性是IPagedList&lt;Profile&gt;。并且拥有另一个分页列表的分页列表是没有意义的。你的模型应该是@model ProfilesListViewModel,然后是@foreach (var p in Model.Profiles) { ... },最后是@Html.PagedListPager(Model.Profiles, page ....
  • 非常感谢,您的解决方案有效,我只是搞砸了模型,感谢

标签: asp.net-mvc-4 asp.net-mvc-viewmodel pagedlist


【解决方案1】:

PagedList 应该在与此类似的地方实现。无需为 PagedList 创建视图模型。

控制器

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

查看

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

【讨论】:

    最近更新 更多