我是asp.net mvc的初学者。
我有一个产品型号,我希望为任何产品实施用户评论。 我希望将注释显示为每个级别的树。
我在视图中使用递归方法来显示评论和回复评论但是 它无法在第三级显示回复评论。
显示replyId为空时的第一级术语。首先显示对第一条评论的回复。但是之前的回复答案没有在第三级显示。
请帮帮我。非常感谢你。
这是我的模特
namespace Mvc_Shop.Models
{
public class Comment
{
public Comment()
{
this.Children = new HashSet<Comment>();
}
public int Id { set; get; }
public string Body { set; get; }
public System.DateTime CommentDate { get; set; }
public int? ReplyId { get; set; }
public ICollection<Comment> Children { get; set; }
public virtual Comment Reply { set; get; }
public string User_Id { get; set; }
[ForeignKey("User_Id")]
public virtual User User { get; set; }
public int Product_Id { get; set; }
[ForeignKey("Product_Id")]
public virtual Product Product { get; set; }
}
}
这是我的观点
@helper TreeView(IEnumerable<Mvc_Shop.Models.Comment> Comments)
{
foreach (var item in Comments)
{
<div class="media">
<div class="media-body" id="Comment_@(item.Id)" style="padding: 3px;width: 100%;">
<h4 class="media-heading">
@item.User.UserName
<label class="badge pull-left" style="font-weight: normal">@item.CommentDate.ToString("dddd, dd MMMM yyyy - HH:mm")</label>
</h4>
<div class="clearfix">
@item.Body
</div>
<fieldset id="reply-cmnt-form_@(item.Id)" class="spr-form-review" style="display:none">
<div class="spr-form-review-body">
@Html.LabelFor(model => model.Comment.Body, new { @class = "spr-form-label" })
@Html.TextAreaFor(model => model.Comment.Body, new { @class = "spr-form-input spr-form-input-text", id = "commentbody" + @item.Id })
@Html.ValidationMessageFor(model => model.Comment.Body)
</div>
</fieldset>
<a id="btn-hide-div_@(item.Id)" class="btn btn-xs pull-left btn-primary" style="clear: both; display:none" onclick="opencmntdiv(@item.Id);"> بستن</a>
<a id="btn-send-div_@(item.Id)" class="btn btn-xs pull-left btn-primary" style="clear: both;float:right;display:none" onclick="ReplyComment(@item.Id);"> ارسال پاسخ</a>
<a id="btn-show-div_@(item.Id)" class="btn btn-xs pull-left btn-primary" style="clear: both" onclick="opencmntdiv(@item.Id);"> پاسخ</a>
</div>
</div>
<hr class="clearfix" />
if (item.Children.Any())
{
@TreeView(item.Children)
}
}
}
@TreeView(Model.Comments)
这是我的行动
public ActionResult ShowProducts(string ProductName)
{
_dbContext = HttpContext.GetOwinContext().Get<ShopContext>();
var model = new UserCommentViewModel();
if (string.IsNullOrEmpty(ProductName))
{
return RedirectToAction("Index");
}
else
{
model.Products = blproduct.Where(p => p.Name.Trim() == ProductName.Trim()).ToList();
model.Comments = (from u1 in _dbContext.Users
join u2 in _dbContext.Comments
on u1.Id equals u2.User.Id
where u2.ReplyId == null
select new {
Id = u2.Id, Body = u2.Body, CommentDate = u2.CommentDate, User = u1,Children = u2.Children
}).ToList().Select(x => new Comment
{
Id = x.Id, Body = x.Body, CommentDate = x.CommentDate, User = x.User,Children = x.Children
}).ToList();
if (model != null)
{
return View(model);
}
else
{
return RedirectToAction("Index");
}
}
}
非常感谢。