c# - Convert Model to ViewModel without automapper -
hi convert ilist of model ilist of viewmodel method
public static ilist<postviewmodel> converttopostviewmodellist(this ilist<post> posts) { return posts.select(converttopostviewmodel).tolist(); }
and 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; }
what problem , got error view :
the model item passed dictionary of type 'system.collections.generic.list`1[blog.domain.model.post]', dictionary requires model item of type 'system.collections.generic.ilist`1[blog.web.ui.viewmodels.postviewmodel]'.
then ?? convert ilist of model viewmodel via :
return posts.select(converttopostviewmodel).tolist();
then going on ??
what have done in action
public actionresult posts() { var blogpost = _blogrepository.getallpost(); var blogpostviewmodel = blogpost.converttopostviewmodellist(); return view("posts", blogpostviewmodel); }
and in view
@model ilist<blog.web.ui.viewmodels.postviewmodel>
two possibilities:
the method posted not 1 being matched. can verify throwing exception in controller action , seeing if gets thrown. result of either: (a) overloaded
posts
controller action, other being matched; or, (b) custom route intercepting request.you returned domain object in testing, after changing controller action set model
postviewmodel
, forgot recompile mvc project. try recompiling solution , see if results change.
Comments
Post a Comment