Reputation: 197
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!
Upvotes: 1
Views: 107
Reputation: 1038720
You need to return an ActionResult from controller actions:
public ActionResult GetAllComment(int _UserID)
{
using (var context = new TourBlogEntities1())
{
var AllComment = from s in context.Comments
where s.UserID == _UserID
select s;
return View(AllComment.ToList());
}
}
Now the corresponding view could be strongly typed to List<Comment>
:
@model List<Comment
and then you could loop through the model and display it:
@foreach (var comment in Model)
{
<div><@comment.Text</div>
}
UPDATE:
If this method is in your BLL you could invoke it in your controller action and pass the result to the view:
public ActionResult SomeAction(int userID)
{
List<Comment> comments = bll.GetAllComment(userID);
return View(comments);
}
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:
public class MyViewModel
{
public List<Comment> Comments { get; set; }
public string Foo { get; set; }
public string Bar { get; set; }
...
}
and then have your controller action pass this view model to the view:
public ActionResult SomeAction(int userID)
{
var model = new MyViewModel();
model.Comments = bll.GetAllComment(userID);
model.Foo = "that's the foo";
model.Bar = "that's the bar";
return View(model);
}
and finally your view is strongly typed to the new view model:
@model MyViewModel
<h2>@Html.DisplayFor(x => x.Foo)</h2>
@foreach (var comment in Model.Comments)
{
<div>@comment.Text</div>
}
<span>@Html.DisplayFor(x => x.Bar)</span>
Upvotes: 1