1
你好,这是我转换一个ILIST模型到视图模型的ILIST方法转换模型视图模型不automapper
public static IList<PostViewModel> ConvertToPostViewModelList(this IList<Post> posts)
{
return posts.Select(ConvertToPostViewModel).ToList();
}
而且这是ConvertToPostViewModel
public static PostViewModel ConvertToPostViewModel(this Post post)
{
var blogPostViewModel = new PostViewModel
{
Id = post.Id,
Body = post.Body,
Summary = post.Summary,
Title = post.Title,
Category = post.Category,
CreationDate = post.CreationDate,
SelectedCategory = post.CategoryId,
SelectedTag = post.TagId,
Tag = post.Tag,
UrlSlug = post.UrlSlug
};
return blogPostViewModel;
}
什么问题,此,我得到这个错误查看:
The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[Blog.Domain.Model.Post]', but this dictionary requires a model item of type 'System.Collections.Generic.IList`1[Blog.Web.UI.ViewModels.PostViewModel]'.
然后??我通过这个将Model的Ilist转换为ViewModel:
return posts.Select(ConvertToPostViewModel).ToList();
那么是怎么回事?
我在行动
public ActionResult Posts()
{
var blogPost = _blogRepository.GetAllPost();
var blogPostViewModel = blogPost.ConvertToPostViewModelList();
return View("Posts", blogPostViewModel);
}
,并查看
@model IList<Blog.Web.UI.ViewModels.PostViewModel>
发布错误引发的代码。 – 2013-05-02 19:01:27
看起来您正在将'Post'传递给您的视图,而不是生成的'PostViewModel'。你有没有更新你的电话到'View()'? – 2013-05-02 19:03:48