分页 - '列表'不包含'PagingInfo'的定义

时间:2017-02-03 15:31:41

标签: c# asp.net-mvc model-view-controller pagination code-first

大家好,所以我正在尝试使用asp.net mvc创建一个应用程序,该代码第一个数据库允许用户能够创建一个包含他们希望的图像的博客文章。我正在尝试制作它所以当有超过4个帖子时,它会分页。代码工作除了这一行

@ Html.PageLinks(Model.PagingInfo,x => Url.Action(“Display”,new {page = x}))

您实际点击查看是否有超过4个帖子

感谢您的帮助。

错误

  

严重级代码说明项目文件行   错误CS1061'List'不包含'PagingInfo'的定义,并且没有扩展方法'PagingInfo'可以找到接受类型'List'的第一个参数(你是否缺少using指令或汇编引用?)Crud

控制器

public int PageSize = 4;
    public ViewResult Display(int page = 1)
            {
                var posts = db.Posts.Select(d => new PostVM()
                {
                    ID = d.ID,
                    Heading = d.Heading,
                    Body = d.Body,
                    Images = d.Images.Select(i => new ImageVM()
                    {
                        Path = i.Path,
                        DisplayName = i.DisplayName
                    }
                                  ).ToList()
                }).ToList().OrderByDescending(p => p.ID)
                .Skip((page - 1) * PageSize)
                .Take(PageSize).ToList();
                return View(posts);
            }

查看

@model List<Crud.Models.PostVM>
@{
    ViewBag.Title = "Index";
}


@foreach (var p in Model)
{
    <h3>@p.Heading</h3>
    <p>@p.Body</p>
    foreach (var image in p.Images)
    {
        <img class="img-thumbnail" width="150" height="150" src="~/Images/@image.Path" />
    }
}

<div>
    @Html.PageLinks(Model.PagingInfo, x => Url.Action("Display", new { page = x }))
</div>

模型

namespace Crud.Models
{
    public partial class PostModel
    {
        public PostModel()
        {
            Images = new List<ImageModel>();
        }
        [Key]
        [HiddenInput(DisplayValue = false)]
        public int ID { get; set; }
        [Required(ErrorMessage = "Heading is Required")]
        [Display(Name = "Heading")]
        public string Heading { get; set; }
        [Required(ErrorMessage = "Body is Required")]
        [DataType(DataType.MultilineText)]
        [Display(Name = "Body")]
        public string Body { get; set; }
        public virtual ICollection<ImageModel> Images { get; set; }
        public IEnumerable<HttpPostedFileBase> File { get; set; }
        public DateTime Date { get; set; }
    }

    public class ImageModel
    {
        [Key]
        public int ID { get; set; }
        public string Path { get; set; }
        public virtual PostModel Post { get; set; }
        public string DisplayName { get; set; }
    }
    public class ImageVM
    {
        public int? ID { get; set; }
        public string Path { get; set; }
        public string DisplayName { get; set; }
        public bool IsDeleted { get; set; }
    }
    public partial class PostVM
    {
        public PostVM()
        {
           Images = new List<ImageVM>();
        }

        public int? ID { get; set; }
        public string Heading { get; set; }
        public string Body { get; set; }
        public IEnumerable<HttpPostedFileBase> Files { get; set; }
        public List<ImageVM> Images { get; set; }
        public DateTime Date { get; set; }
        public IEnumerable<PostModel> Posts { get; set; }
        public PagingInfo PagingInfo { get; set; }
    }
    public class PagingInfo
    {
        public int TotalItems { get; set; }
        public int ItemsPerPage { get; set; }
        public int CurrentPage { get; set; }
        public int TotalPages
        {
            get
            {
                return (int)Math.Ceiling((decimal)TotalItems /
          ItemsPerPage);
            }
        }
    }
}

Html Helper

 public static class PagingHelpers
    {
        public static MvcHtmlString PageLinks(this HtmlHelper html,PagingInfo pagingInfo,
        Func<int, string> pageUrl)
        {
            StringBuilder result = new StringBuilder();
            for (int i = 1; i <= pagingInfo.TotalPages; i++)
            {
                TagBuilder tag = new TagBuilder("a");
                tag.MergeAttribute("href", pageUrl(i));
                tag.InnerHtml = i.ToString();
                if (i == pagingInfo.CurrentPage)
                {
                    tag.AddCssClass("selected");
                    tag.AddCssClass("btn-primary");
                }
                tag.AddCssClass("btn btn-default");
                result.Append(tag.ToString());
            }
            return MvcHtmlString.Create(result.ToString());
        }
    }

存储库

  public interface IRepository
    {
        IEnumerable<PostModel> Posts { get; }

    }

尝试建议的解决方案

视图

    @model Crud.Models.PagedList

模特

    public class PagedList
    {
        public PagingInfo PagingInfo { get; set; }
        public List<PostVM> Posts { get; set; }
    }

错误:无法运行

  

foreach语句不能对'Crud.Models.PagedList'类型的变量进行操作,因为'Crud.Models.PagedList'不包含'GetEnumerator'的公共定义
          }

视图

    @model Crud.Models.PagedList<Crud.Models.PostVM>

模特

 public class PagedList<T> : List<T>
    {
        public PagingInfo PagingInfo { get; set; }
        public List<PostVM> Posts { get; set; }
    }

错误:运行时

  

传递到字典中的模型项的类型为'System.Collections.Generic.List 1[Crud.Models.PostVM]', but this dictionary requires a model item of type 'Crud.Models.PagedList 1 [Crud.Models.PostVM]'。

0 个答案:

没有答案