我有以下实体和模型:

实体:

public class BlogPost {
    public virtual int Id { get; set; }
    public virtual IList<Keyword> Keywords { get; set; }
    public virtual IList<BlogComment> Comments { get; set; }
}

public class BlogComment {
    public virtual int Id { get; set; }
    public virtual BlogPost Post { get; set; }
}

public class Keyword {
    public virtual int Id { get; set; }
    public virtual IList<BlogPost> BlogPosts { get; set; }
}


视图模型:

public class BlogPostModel {
    public int Id { get; set; }
    // instead of IList<BlogComment> I have this:
    public int CommentsCount { get; set; }
    public IList<KeywordModel> Keywords { get; set; }
}

public class KeywordModel {
    public int Id { get; set; }
    public IList<BlogPostModel> Posts { get; set; }
}

public class BlogCommentModel {
    public int Id { get; set; }
    public BlogPostModel Post { get; set; }
}


现在,我想加载博客文章的页面列表及其关键字和评论数,并通过AutoMapper库将它们映射到IPagedList<BlogPostModel>。你能帮我吗?我正在使用nuget上的Mvc IPagedList

public interface IPagedList<out T> : IPagedList, IEnumerable<T> {
    T this[int index] { get; }
    int Count { get; }
    IPagedList GetMetaData();
}

public interface IPagedList {
    int PageCount { get; }
    int TotalItemCount { get; }
    int PageNumber { get; }
    int PageSize { get; }
    bool HasPreviousPage { get; }
    bool HasNextPage { get; }
    bool IsFirstPage { get; }
    bool IsLastPage { get; }
    int FirstItemOnPage { get; }
    int LastItemOnPage { get; }
}


我测试了很多方法并用Google搜索了问题,但找不到任何解决方案。感谢您的任何建议。

最佳答案

我遇到了同样的问题,并且找到了解决问题的方法,也许这种方法也可以帮到您。

    public class GenericModelViewConverter<TModel, TView> : IModelViewConverter<TModel, TView>
        where TModel : class
        where TView : class
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="GenericModelViewConverter{TModel, TView}"/> class.
        /// </summary>
        public GenericModelViewConverter()
        {
            // It is not he best place to create the mapping here
            Mapper.CreateMap<TView, TModel>();
            Mapper.CreateMap<TView, TModel>().ReverseMap();
        }

        /// <summary>
        /// Converts the view to model.
        /// </summary>
        /// <param name="view">The view as a source.</param>
        /// <param name="model">The model as a destination value.</param>
        /// <returns>The destination model.</returns>
        public virtual TModel ConvertViewToModel(TView view, TModel model = null)
        {
            return model == null ? Mapper.Map<TView, TModel>(view) : Mapper.Map(view, model);
        }

        /// <summary>
        /// Converts the model to view.
        /// </summary>
        /// <param name="model">The model as a source.</param>
        /// <param name="view">The view as a destination.</param>
        /// <returns>The destination view.</returns>
        public virtual TView ConvertModelToView(TModel model, TView view = null)
        {
            return view == null ? Mapper.Map<TModel, TView>(model) : Mapper.Map(model, view);
        }

        /// <summary>
        /// Converts the view to model.
        /// </summary>
        /// <param name="viewCollection">The collection of views.</param>
        /// <returns>The collection of models.</returns>
        public virtual IEnumerable<TModel> ConvertViewToModel(IEnumerable<TView> viewCollection)
        {
            return Mapper.Map<IEnumerable<TView>, HashSet<TModel>>(viewCollection);
        }

        /// <summary>
        /// Converts the model to view.
        /// </summary>
        /// <param name="modelCollection">The collection of models.</param>
        /// <returns>The collection of views.</returns>
        public virtual IEnumerable<TView> ConvertModelToView(IEnumerable<TModel> modelCollection)
        {
            return Mapper.Map<IEnumerable<TModel>, HashSet<TView>>(modelCollection);
        }

        /// <summary>
        /// Converts the model to view.
        /// </summary>
        /// <param name="modelCollection">The model collection.</param>
        /// <returns>The collection of models.</returns>
        public virtual IPagedCollection<TView> ConvertModelToView(IPagedCollection<TModel> modelCollection)
        {
            var list = modelCollection.ToList();
            var resultList = ConvertModelToView(list);

            return new PagedCollection<TView>(resultList, modelCollection.PageIndex, modelCollection.PageSize, modelCollection.TotalItemCount);
        }

        /// <summary>
        /// Converts the view to model.
        /// </summary>
        /// <param name="viewCollection">The view collection.</param>
        /// <returns>The collection of views.</returns>
        public virtual IPagedCollection<TModel> ConvertViewToModel(IPagedCollection<TView> viewCollection)
        {
            var list = viewCollection.ToList();
            var resultList = ConvertViewToModel(list);

            return new PagedCollection<TModel>(resultList, viewCollection.PageIndex, viewCollection.PageSize, viewCollection.TotalItemCount);
        }
    }

09-27 23:07