我有以下类,我需要将 Thread 类中的帖子集合映射到 ThreadView 类中的分页帖子集合,但我完全不知道如何去做。
// Database class
public class Thread
{
public virtual int Id { get; set; }
public virtual string Title { get; set; }
public virtual IEnumerable<Post> Posts { get; set;}
}
// View class
public class ThreadView
{
public int Id { get; set; }
public string Title { get; set; }
public PaginatedList<PostView> Posts { get; set; }
}
public class PaginatedList<T> : List<T>
{
public PaginatedList<IEnumerable<T> source, int page)
{
...
}
}
我的映射很简单:
Mapper.CreateMap<Thread, ThreadView>();
Mapper.CreateMap<Post, PostView>();
我的操作方法如下所示:
public ViewResult ViewThread(int threadId, int page = 1)
{
var thread = _forumService.GetThread(threadId, page);
var viewModel = Mapper.Map<Thread, ThreadView>(thread);
return View(viewModel);
}
但这显然行不通。任何人都可以帮忙吗?
谢谢
更新
我想我现在会满足于这样做,即使它闻起来有点:
public ViewResult ViewThread(int id, int page = 1)
{
var thread = _forumService.GetThread(id, page);
var posts = Mapper.Map<IEnumerable<Post>, IEnumerable<PostView>>(thread.Posts);
var viewModel = new ThreadView {
Id = thread.Id,
Title = thread.Title,
Posts = new PaginatedList<PostView>(posts, page)
};
return View(viewModel);
}
除非其他人知道如何做到这一点?