0

我有两个模型,如下所示:

一:

 [Validator(typeof(BlogPostValidator))]
    public partial class BlogPostModel : BaseNopEntityModel
    {
        public BlogPostModel()
        {
            Tags = new List<string>();
            Comments = new List<BlogCommentModel>();
            AddNewComment = new AddBlogCommentModel();
        }

        public string SeName { get; set; }

        public string Title { get; set; }

        public string Body { get; set; }

        public bool AllowComments { get; set; }

        public int NumberOfComments { get; set; }

        public DateTime CreatedOn { get; set; }

        public IList<string> Tags { get; set; }

        public IList<BlogCommentModel> Comments { get; set; }
        public AddBlogCommentModel AddNewComment { get; set; }

        public BlogCommentModel blogcommentmodel { get; set; }
    }

二:

 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 IList<BlogComment> ChildCommentList { get; set; }//netra
    }

我想利用BlogCommentModel中的ChildCommentList来显示嵌套评论。

我的观点:

@model BlogPostModel
@using Nop.Web.Models.Blogs;
 @if (Model.AllowComments)
        {
            <div class="clear">
            </div>
            <fieldset class="new-comment" id="addcomment">
                <legend class="title">@T("Blog.Comments.LeaveYourComment")</legend>
                @using (Html.BeginForm())
                {
                    <div>
                        <div class="message-error">@Html.ValidationSummary(true)</div>
                        @{
                    string result = TempData["nop.blog.addcomment.result"] as string;
                        }
                        @if (!String.IsNullOrEmpty(result))
                        {
                            <div class="result">@result</div>
                        }
                        <div class="forms-box">
                            <div class="inputs">
                                @Html.LabelFor(model => model.AddNewComment.CommentText)
                                <div class="input-box">
                                    @Html.TextAreaFor(model => model.AddNewComment.CommentText, new { @class = "comment-text" })
                                </div>
                                @Html.ValidationMessageFor(model => model.AddNewComment.CommentText)
                            </div>
                            @if (Model.AddNewComment.DisplayCaptcha)
                            {
                                <div class="captcha-box">
                                    @Html.Raw(Html.GenerateCaptcha())
                                </div>
                                <div class="clear">
                                </div>
                            }
                        </div>
                        <div class="clear">
                        </div>
                        <div class="buttons">
                            <input type="submit" name="add-comment" class="button-1 blog-post-add-comment-button" value="@T("Blog.Comments.SubmitButton")" />
                        </div>
                    </div>
                }
            </fieldset>
                if (Model.Comments.Count > 0)
                {
            <div class="clear">
            </div>
            <div class="comment-list">
                <div class="title">
                    @T("Blog.Comments")
                </div>
                <div class="clear">
                </div>
                @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>
                    <div>
                     @foreach(var childcomments in Model.blogcommentmodel.ChildCommentList)
                     {
                      @Html.Raw(Nop.Core.Html.HtmlHelper.FormatText(childcomments.CommentText, false, true, false, false, false, false))
                     }
                    </div>
                }
                <div class="buttons">
                    <input type="submit" id="replyto" name="reply-comment" class="button-1 blog-post-add-comment-button" value="@T("Blog.Comments.ReplyButton")" />
                </div>
            </div>
                }
        }

发生错误:

@foreach(var childcomments in Model.blogcommentmodel.ChildCommentList)
                         {
                          @Html.Raw(Nop.Core.Html.HtmlHelper.FormatText(childcomments.CommentText, false, true, false, false, false, false))
                         }
                        </div>
                    }

用户代码未处理 NullReferenceException -{“对象引用未设置为对象的实例。”}

我的 ActionResult 控制器代码:

public ActionResult BlogPost(int blogPostId)
        {
            if (!_blogSettings.Enabled)
                return RedirectToRoute("HomePage");

            var blogPost = _blogService.GetBlogPostById(blogPostId);
            if (blogPost == null ||
                (blogPost.StartDateUtc.HasValue && blogPost.StartDateUtc.Value >= DateTime.UtcNow) ||
                (blogPost.EndDateUtc.HasValue && blogPost.EndDateUtc.Value <= DateTime.UtcNow))
                return RedirectToRoute("HomePage");

            var model = new BlogPostModel();
            **PrepareBlogPostModel(model, blogPost, true);**

            return View(model);
        }

在上述 ActionResult 中调用的以下方法:

 [NonAction]
        protected void PrepareBlogPostModel(BlogPostModel model, BlogPost blogPost, bool prepareComments)
        {
            if (blogPost == null)
                throw new ArgumentNullException("blogPost");

            if (model == null)
                throw new ArgumentNullException("model");

            model.Id = blogPost.Id;
            model.SeName = blogPost.GetSeName();
            model.Title = blogPost.Title;
            model.Body = blogPost.Body;
            model.AllowComments = blogPost.AllowComments;
            model.CreatedOn = _dateTimeHelper.ConvertToUserTime(blogPost.CreatedOnUtc, DateTimeKind.Utc);
            model.Tags = blogPost.ParseTags().ToList();
            model.NumberOfComments = blogPost.ApprovedCommentCount;
            model.AddNewComment.DisplayCaptcha = _captchaSettings.Enabled && _captchaSettings.ShowOnBlogCommentPage;
            if (prepareComments)
            {
               // var blogchildcomment = _blogService.GetAllChildComments();
                var blogComments = blogPost.BlogComments.Where(pr => pr.IsApproved).OrderBy(pr => pr.CreatedOnUtc);
                foreach (var bc in blogComments)
                {
                    var commentModel = new BlogCommentModel()
                    {
                        Id = bc.Id,
                        CustomerId = bc.CustomerId,
                        CustomerName = bc.Customer.FormatUserName(),
                        CommentText = bc.CommentText,
                        CreatedOn = _dateTimeHelper.ConvertToUserTime(bc.CreatedOnUtc, DateTimeKind.Utc),
                        AllowViewingProfiles = _customerSettings.AllowViewingProfiles && bc.Customer != null && !bc.Customer.IsGuest(),
                        CommentParentID = bc.CommentParentID,//Netra
                    };
                    //Netra
                    var comments = _blogService.GetBlogComments(bc.CommentParentID);
                    if (comments != null)
                    {
                        commentModel.ChildCommentList = new List<BlogComment>(); 
                        foreach (var cmnt in comments)
                        {
                            commentModel.ChildCommentList.Add(cmnt);
                        }
                    }

                    if (_customerSettings.AllowCustomersToUploadAvatars)
                    {
                        var customer = bc.Customer;
                        string avatarUrl = _pictureService.GetPictureUrl(customer.GetAttribute<int>(SystemCustomerAttributeNames.AvatarPictureId), _mediaSettings.AvatarPictureSize, false);
                        if (String.IsNullOrEmpty(avatarUrl) && _customerSettings.DefaultAvatarEnabled)
                            avatarUrl = _pictureService.GetDefaultPictureUrl(_mediaSettings.AvatarPictureSize, PictureType.Avatar);
                        commentModel.CustomerAvatarUrl = avatarUrl;
                    }
                    model.Comments.Add(commentModel);
                }
            }
        } 

BlogComment 是描述属性的类。

 public partial class BlogComment : CustomerContent
    {
        /// <summary>
        /// Gets or sets the comment text
        /// </summary>
        public virtual string CommentText { get; set; }

        /// <summary>
        /// Gets or sets the blog post identifier
        /// </summary>
        public virtual int BlogPostId { get; set; }

        /// <summary>
        /// Gets or sets the blog post
        /// </summary>
        public virtual BlogPost BlogPost { get; set; }

        public virtual int CommentParentID { get; set; } //netra
    }
4

3 回答 3

0

所以你在某处有一个空值——你需要找出什么是空值并使它不为空;或将 Razor 代码更改为:

@if(Model.blogcommentmodel != null && Model.blogcommentmodel.ChildCommentList != null)
{
  @* original @foreach statement here *@
}
于 2013-01-02T12:34:28.950 回答
0

我的第一个猜测是您需要为模型创建一个默认构造函数。在构造函数中,设置ChildCommentList = new List<BlogComment>();.

看起来你没有在你的控制器中设置它,你设置模型的方式会导致 ChildCommentList 仍然为空。使用该默认构造函数,如果您错过在某处设置它,您将不必担心代码会像这样被破坏。

为了回答 wnetra 的评论,构造函数位于 BlogcommentModel 类上。所以你需要这样的东西:

public class BlogcommentModel {
    /* All of the property declarations */
    public IList<BlogComment> ChildCommentList { get; set; }

    /* Constructor for the BlogcommentModel class */
    public BlogcommentModel() {
        ChildcommentList = new List<BlogComment>();
    }
}

任何引用对象都应始终在默认构造函数中实例化,以确保在尝试在代码中引用它们时它们不会为空。

另请参阅 Andras 关于!= null尝试在代码中引用引用对象时总是做的回答。

于 2013-01-02T12:34:49.997 回答
0

我从 BlogCommenmodel 中删除了以下代码:

//public int CommentParentID { get; set; }

    //public IList<BlogComment> ChildCommentList { get; set; }//netra

    //public BlogCommentModel()
    //{
    //    ChildCommentList = new List<BlogComment>();
    //}

并在 BlogPostModel 中引入

 [Validator(typeof(BlogPostValidator))]
public partial class BlogPostModel : BaseNopEntityModel
{
    public BlogPostModel()
    {
        Tags = new List<string>();
        Comments = new List<BlogCommentModel>();
        AddNewComment = new AddBlogCommentModel();
        ChildCommentList = new List<BlogComment>();
    }

    public string SeName { get; set; }

    public string Title { get; set; }

    public string Body { get; set; }

    public bool AllowComments { get; set; }

    public int NumberOfComments { get; set; }

    public DateTime CreatedOn { get; set; }

    public IList<string> Tags { get; set; }

    public IList<BlogCommentModel> Comments { get; set; }
    public AddBlogCommentModel AddNewComment { get; set; }


    //Netra
    public int CommentParentID { get; set; }

    public IList<BlogComment> ChildCommentList { get; set; }//netra

   // public BlogCommentModel blogcommentmodel { get; set; }
}

在视图中:

<div>
                            @if (Model.ChildCommentList != null)
                            {
                                foreach (var childcomments in Model.ChildCommentList)
                                {
                                    @Html.Raw(Nop.Core.Html.HtmlHelper.FormatText(childcomments.CommentText, false, true, false, false, false, false))
                                }
                            }
                        </div>
于 2013-01-03T07:32:24.690 回答