I have method to retrieve data from DAL as follows:
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>();
}
}
Now I’m trying to call this method from my controller as follwos:
[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);
}
I bind my view with the model, but could not use the list, Even if I try to pass the list itself to view, still I can’t use that list in view!
My model class is as follows:
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; }
}
So how to use the list in view? I need the help badly!
You need to return an ActionResult from controller actions:
Now the corresponding view could be strongly typed to
List<Comment>:and then you could loop through the model and display it:
UPDATE:
If this method is in your BLL you could invoke it in your controller action and pass the result to the view:
UPDATE 2:
If you need to pass other properties to the view in addition to this list then you define a view model containing all the necessary information:
and then have your controller action pass this view model to the view:
and finally your view is strongly typed to the new view model: