Full-Stack freelance Umbraco developer in the UK.

George Phillipson - Freelance Umbraco developer

Review visitor comments before publishing to your website Umbraco 7

public abstract class CommentFormModel
    {
        [DisplayName("Title")]
        public string MessageTitle { get; set; }
        [DisplayName("Message")]
        public string VisitorMessage { get; set; }
    }
    public class CommentsModel : CommentFormModel
    {
  
    }
    public class DisplayCommentsModel : CommentFormModel
    {
        public DisplayCommentsModel(string title, string message)
        {
            MessageTitle    = title;
            VisitorMessage  = message;
        }
    }
public ActionResult CommentsForm()
        {
            
            return PartialView("~/Views/Partials/pvCommentsForm.cshtml");
        }
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult CommentsForm(CommentsModel model)
        {
            try
            {
            CreateBlogMessage(model);
            TempData["Message"] = "Your post has been received";
            return RedirectToCurrentUmbracoPage();
            }
            catch (Exception)
            {
                TempData["Message"] = "There was a problem submitting your post";
                return RedirectToCurrentUmbracoPage();
            }
        }
        private void CreateBlogMessage(CommentsModel model)
        {
                var contentService  = ApplicationContext.Current.Services.ContentService;
                var pageName        = CurrentPage.Name;
                var allEventsNode   = GetAllEventsNode();
                var content = contentService.CreateContent(pageName, allEventsNode.Id, "reviewComments");
                content.SetValue("messageTitle", model.MessageTitle);
                content.SetValue("visitorMessage", model.VisitorMessage);
                contentService.SaveAndPublishWithStatus(content);
        }
        private IPublishedContent GetAllEventsNode()
        {
            return Umbraco.TypedContentAtRoot().DescendantsOrSelf("reviewComments").FirstOrDefault();
        }
        [HttpGet]
        public ActionResult DisplayComments()
        {
            try
            {
                IEnumerable<IPublishedContent> posts = CurrentPage.AncestorOrSelf(1).DescendantsOrSelf("reviewComments").ToList();
                List<DisplayCommentsModel> model = new List<DisplayCommentsModel>();
                foreach (IPublishedContent content in posts.Where(x => x.GetPropertyValue<bool>("commentsReviewed")))
                {
                    string messageTitle     = content.GetPropertyValue<string>("messageTitle");
                    string visitorMessage   = content.GetPropertyValue<string>("visitorMessage");
                   
                    model.Add(new DisplayCommentsModel(messageTitle, Regex.Replace(visitorMessage, Environment.NewLine, "<br/>")));
                }
                return PartialView("~/Views/Partials/pvDisplayComments.cshtml", model);
            }
            catch (Exception)
            {
                TempData["Message"] = "There was a problem displaying posts";
                return CurrentUmbracoPage();
            }
        }
@inherits UmbracoViewPage<List<Microdata.Models.DisplayCommentsModel>>
<div class="container">
    @if (Model.Any())
    {
        <div class="row">
            <div class="col-md-12">
                @foreach (var m in Model.ToList())
                {
                    <div>
                        <div class="row">
                            <div class="col-md-6">
                                <h4>
                                    @m.MessageTitle
                                </h4>
                                <p>@Html.Raw(m.VisitorMessage)</p>
                            </div>
                        </div>
                        <hr />
                    </div>
                }
            </div>
        </div>
    }
</div>

We now have all the code in place and ready to test, you should see the home page as shown in the image. Complete the form and then log in to Umbraco and click on visitor comments.

You should now see the post you just submitted.

Click on the name and then content and you will now see the title and message of the post.

Click the comments reviewed checkbox and then save and publish, then refresh the home page, your comment is now visible. 

Comments Page 1 of 1 (3 Posts)

sir what is the code used for below the following line"As you can see in the controller, I have an ActionResult for displaying comments, which checks for the boolean value 'commentsReviewed', if you go back to the Document Type image, you can see the checkbox for this value." <br/>in which files we have to add it/

Comment posted by Ishan on 21 Nov 2018 13:04


Hi Ishan

If you add it to the document type that you add your comments too, then just check for a value of true to display the comments. 

Or visit https://github.com/GeorgePhillipson/Microdata and download code


Thank you George Phillipson sir i have cleared my doubts from the example shown in the link and i think i will get it working on my system soon.

Comment posted by Ishan on 22 Nov 2018 05:53


Hi Ishan

Good to hear, one thing I would point out is this post in old in Umbraco terms and now we have nested content which is much better in my opinion

George


OK sir, thanks for pointing out ,i will look upon it. I am a newbie to umbraco so just trying my skills with basics once i get used to it i shall dig into some PRO stuff's.

Comment posted by Ishan on 22 Nov 2018 08:52


Please enter your comment.