我有从 DAL 检索数据的方法,如下所示:
public List<Comment> GetAllComment(int _UserID)
{
using (var context = new TourBlogEntities1())
{
var AllComment = from s in context.Comments
where s.UserID == _UserID
select s;
return AllComment.ToList<Comment>();
}
}
现在我试图从我的控制器调用这个方法,如下所示:
[HttpPost]
public ActionResult NewComment(PostComment model)
{
var business = new Business();
var entity = new Comment();
entity.Comments = model.Comments.Comments;
//entity.PostID = model.Posts.PostID; HOW TO PASS POSTID???
entity.UserID = business.GetUserID(User.Identity.Name);
business.PostComment(entity);
var CommentsListFromEntity = business.GetAllComment(business.GetUserID(User.Identity.Name));
var viewmodel = new PostComment();
viewmodel.CommentsListFromModel = CommentsListFromEntity.ToList<CommentInfo>(); // Error is showing here
return View("ViewPost", viewmodel);
}
我将视图与模型绑定,但无法使用列表,即使我尝试将列表本身传递给视图,我仍然无法在视图中使用该列表!
我的模型类如下:
public class PostComment
{
public PostComment()
{
Posts = new NewPost();
Comments = new CommentInfo();
User = new UserInfoModel();
}
public NewPost Posts { get; set; }
public CommentInfo Comments { get; set; }
public UserInfoModel User { get; set; }
public List<CommentInfo> CommentsListFromModel { get; set; }
}
那么如何使用视图中的列表呢?我非常需要帮助!