@foreach(comment in Model.Comments.Where(x => x.CommentParentID == null)
{
//LOGIC TO SHOW COMMENTS
@foreach(cmt in Model.Comments.Where(x => x.CommentParentID == comment.ID)
{
//LOGIC TO SHOW REPLY'S
}
}
this should work altough i didn't see any commentID in ur model except for the parentID
hope this example helped you :)
Challenge the best, die like the rest.
Marked as answer by netraswable on Jan 03, 2013 11:33 AM
@foreach(cmt in parent.ChildCommentList) { //LOGIC TO SHOW REPLY'S FIRST CHILD OBJECT }
}
hmm this is as far as i got without intellisense i made a mistake somewhere but i'll check by tomorrow on how this works got to think this thru myself :/
@foreach(cmt in parent.ChildCommentList)
{
//LOGIC TO SHOW REPLY'S FIRST CHILD OBJECT
}
}
hmm this is as far as i got without intellisense i made a mistake somewhere but i'll check by tomorrow on how this works got to think this thru myself :/
What i did is following:
@{var parent = new BlogCommentModel();}
@foreach (var comment in Model.Comments)
{
@foreach (var child in Model.Comments.Where(x => x.CommentParentID == comment.Id))
{
if (child.ChildCommentList!= null || child.ChildCommentList.Any())
{
parent = child;
foreach (var cmt in parent.ChildCommentList)
{
}
}
}
<div></div>
when ChildCommentList is null. it is throw an exception :
ArgumentNullException was unhandled by user code. - {"Value cannot be null.\r\nParameter name: source"}
ok let's see been thinking about this but it's hard in view because you got to create some sort of endless loop.
so the solution must be made controllerside
public ActionResult blabla()
{
ViewBag.Comments = getBlogComments(entitycontext.BlogComments.All());
return View(entitycontext.BlogComments.All()) //you probaply don't need a model in this view anymore..
}
StringBuilder c = new StringBuilder();
public HtmlString getBlogComments(List<BlogComments> Comments)
{
c.clear();
foreach(comment in Comments)
{
c.Append("your html for main comments {0}", comment.text); //parameters like {0}... not sure this works on append tho
if(comment.ChildCommentList != null || comment.ChildCommentList.Any())
{
ReplyComment(comment.ChildCommentList);
}
}
return new HtmlString(c.ToString());
}
public void ReplyComment(List<BlogComments> Comments)
{
foreach(comment in Comments)
{
c.Append("your html for reply comments {0}", comment.text);
if(comment.ChildCommentList != null || comment.ChildCommentList.Any())
{
ReplyComment(comment.ChildCommentList);
}
}
}
i forgot to mention you can also use a dictionary to check if the comment is already been added to stringbuilder
make a dictionary of bools and then set true with the right blogid in dictionary that way you can check dictionary if id is true or false and make the choice to add to stringbuilder or not
the null error can be fixed by something like
if (comment.ChildCommentList == null)
{
comment.ChildCommentList = new List<BlogComments>();
}
netraswable
Member
31 Points
43 Posts
Displaying nested comments in MVC 4 blog
Jan 03, 2013 11:00 AM|LINK
Hello All,
My Model:
public partial class BlogCommentModel: BaseNopEntityModel {public int CustomerId { get; set; } public string CustomerName { get; set; } public string CustomerAvatarUrl { get; set; } public string CommentText { get; set; } public DateTime CreatedOn { get; set; } public bool AllowViewingProfiles { get; set; } public int CommentParentID { get; set; } public BlogCommentModel[] ChildCommentList { get; set; } }@foreach (var comment in Model.Comments) { <div class="blog-comment"> <div class="comment-info"> <div class="user-info"> @if (comment.AllowViewingProfiles) { <a href="@Url.RouteUrl("CustomerProfile", new { id = comment.CustomerId })" class="username">@(comment.CustomerName)</a> } else { <span class="username">@(comment.CustomerName)</span> } <div class="avatar"> @if (!String.IsNullOrEmpty(comment.CustomerAvatarUrl)) { <img src="@(comment.CustomerAvatarUrl)" class="avatar-img" title="avatar" alt="avatar" /> } </div> </div> </div> <div class="comment-content"> <div class="comment-time"> @T("Blog.Comments.CreatedOn"): <span class="stat-value">@comment.CreatedOn.ToString("g")</span> </div> <div class="comment-body"> @Html.Raw(Nop.Core.Html.HtmlHelper.FormatText(comment.CommentText, false, true, false, false, false, false)) </div> </div> @Html.Widget("blogpost_page_inside_comment") </div> <div class="clear"> </div> }sp00k
Participant
1916 Points
435 Posts
Re: Displaying nested comments in MVC 4 blog
Jan 03, 2013 11:14 AM|LINK
@foreach(comment in Model.Comments.Where(x => x.CommentParentID == null) { //LOGIC TO SHOW COMMENTS @foreach(cmt in Model.Comments.Where(x => x.CommentParentID == comment.ID) { //LOGIC TO SHOW REPLY'S } }this should work altough i didn't see any commentID in ur model except for the parentID
hope this example helped you :)
netraswable
Member
31 Points
43 Posts
Re: Displaying nested comments in MVC 4 blog
Jan 03, 2013 11:33 AM|LINK
@sp00k: THanks a lot from past 4 days i am looking for this.
Thanks a lot again.
sp00k
Participant
1916 Points
435 Posts
Re: Displaying nested comments in MVC 4 blog
Jan 03, 2013 11:42 AM|LINK
np that's why we're here :):)
netraswable
Member
31 Points
43 Posts
Re: Displaying nested comments in MVC 4 blog
Jan 03, 2013 11:45 AM|LINK
One more question:
If Child Comments has childcomments then how to display that?
Ex:
In above table ID=2 is achild of ID=1 and ID=4 is child of id=2.
sp00k
Participant
1916 Points
435 Posts
Re: Displaying nested comments in MVC 4 blog
Jan 03, 2013 12:07 PM|LINK
@{ var parent = new BlogCommentModel(); }@foreach(comment in Model.Comments) { //LOGIC TO SHOW COMMENTShmm this is as far as i got without intellisense i made a mistake somewhere but i'll check by tomorrow on how this works got to think this thru myself :/
netraswable
Member
31 Points
43 Posts
Re: Displaying nested comments in MVC 4 blog
Jan 03, 2013 12:16 PM|LINK
Ok.Will check this on my side too.
Thanks
raduenuca
All-Star
24675 Points
4250 Posts
Re: Displaying nested comments in MVC 4 blog
Jan 03, 2013 12:17 PM|LINK
You can use something like this (needs some adapting to your code):
http://weblogs.asp.net/raduenuca/archive/2011/04/24/asp-net-mvc-displaying-a-tree-view-using-a-recursive-declarative-helper-and-jquery.aspx
Radu Enuca | Blog
netraswable
Member
31 Points
43 Posts
Re: Displaying nested comments in MVC 4 blog
Jan 03, 2013 12:39 PM|LINK
sp00k
Participant
1916 Points
435 Posts
Re: Displaying nested comments in MVC 4 blog
Jan 04, 2013 06:42 AM|LINK
ok let's see been thinking about this but it's hard in view because you got to create some sort of endless loop.
so the solution must be made controllerside
public ActionResult blabla() { ViewBag.Comments = getBlogComments(entitycontext.BlogComments.All()); return View(entitycontext.BlogComments.All()) //you probaply don't need a model in this view anymore.. } StringBuilder c = new StringBuilder(); public HtmlString getBlogComments(List<BlogComments> Comments) { c.clear(); foreach(comment in Comments) { c.Append("your html for main comments {0}", comment.text); //parameters like {0}... not sure this works on append tho if(comment.ChildCommentList != null || comment.ChildCommentList.Any()) { ReplyComment(comment.ChildCommentList); } } return new HtmlString(c.ToString()); } public void ReplyComment(List<BlogComments> Comments) { foreach(comment in Comments) { c.Append("your html for reply comments {0}", comment.text); if(comment.ChildCommentList != null || comment.ChildCommentList.Any()) { ReplyComment(comment.ChildCommentList); } } }i forgot to mention you can also use a dictionary to check if the comment is already been added to stringbuilder
make a dictionary of bools and then set true with the right blogid in dictionary that way you can check dictionary if id is true or false and make the choice to add to stringbuilder or not
the null error can be fixed by something like
if (comment.ChildCommentList == null)
{
comment.ChildCommentList = new List<BlogComments>();
}