Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6737235
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:13:31+00:00 2026-05-26T11:13:31+00:00

I’m trying to add a form to allow users to comment on posts on

  • 0

I’m trying to add a form to allow users to comment on posts on my blogging application. So far, I’ve added a form to the post details view and I can submit comments, adding them to my database correctly. However, I have a problem with displaying validation errors to the user. The comment form is contained within a partial view and is rendered using Html.RenderAction inside the post details view. I’d like to stress that I don’t want to use AJAX for this as I’d like to approach this from a progressive enhancement point-of-view.

Here’s the relevant posting action:

[HttpPost, Authorize]
public ActionResult AddComment(CommentViewModel newComment)
{
    if (ModelState.IsValid)
    {
        Comment comment = new Comment(_userRepository.GetByUsername(User.Identity.Name));
        Mapper.Map(newComment, comment);

        _commentRepository.Add(comment);

        _postsRepository.CommentAdded(comment.Article);

        return RedirectToAction("Index", new { id = newComment.PostID });
    }

    // What do I do here?
}

I’ve tried several ways of returning views here but my issue is further complicated by some controller parameter validation that I have going on in the parent action:

//
// GET: /Posts/5/this-is-a-slug

public ActionResult Index(int id, string slug)
{
    PostViewModel viewModel = new PostViewModel();
    var model = _postsRepository.GetByID(id);

    if (model != null)
    {
        if (slug == null || slug.CompareTo(model.Slug) != 0)
        {
            return RedirectToActionPermanent("Index", new { id, slug = model.Slug });
        }
        else
        {
            _postsRepository.PostVisited(model);

            Mapper.Map(model, viewModel);

            viewModel.AuthorName = _userRepository.GetById(model.AuthorID);
        }
    }

    return View(viewModel);
}

This action basically mimics how SO’s URLs work. If a post ID is supplied, the post is fetched from the database along with a slug which is created when the post is created. If the slug in the URL doesn’t match the one in the database, it redirects to include the slug. This is working nicely but it does mean I’m having issues trying to populate my parent viewmodel, which is the following:

public class PostViewModel
{
    public int PostID { get; set; }
    public string Title { get; set; }
    public string Body { get; set; }
    public string Slug { get; set; }
    public DateTime DatePublished { get; set; }
    public int NumberOfComments { get; set; }
    public int AuthorID { get; set; }
    public string AuthorName { get; set; }

    public List<CommentViewModel> Comments { get; set; }
    public CommentViewModel NewComment { get; set; }
}

What I was hoping would work is to populate PostViewModel.NewComment, test to see if it has data and then using it to display any model errors. Unfortunately, I’m lost as to how to accomplish that. This question helped me shape my approach, but it didn’t quite answer my problem.

Could someone give me a gentle push in the right direction? If my approach seems unreasonable, I’d love to find out why and what a potential fix would be.

Many thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T11:13:31+00:00Added an answer on May 26, 2026 at 11:13 am

    Forgot to fill in my answer here. For anyone that happens to stumble on this, the answer was to use TempData to store the ModelState errors and then repopulating ModelState in the relevant controller action.

    Firstly, I declared a key in the controller which would be used to reference the data inside TempData. I decided to base this on the CommentViewModel type as both actions depend on it.

    public class PostsController : Controller
    {
        private static readonly string commentFormModelStateKey = typeof(CommentViewModel).FullName;
        // Rest of class.
    }
    

    In this first action, the code checks to see if TempData contains data assigned to the key. If it does, it’s copied into ModelState.

    // GET: /posts/comment
    [ChildActionOnly]
    public PartialViewResult Comment(PostViewModel viewModel)
    {
        viewModel.NewComment = new CommentViewModel(viewModel.PostID, viewModel.Slug);
    
        if (TempData.ContainsKey(commentFormModelStateKey))
        {
            ModelStateDictionary commentModelState = TempData[commentFormModelStateKey] as ModelStateDictionary;
            foreach (KeyValuePair<string, ModelState> valuePair in commentModelState)
                ModelState.Add(valuePair.Key, valuePair.Value);
        }
    
        return PartialView(viewModel.NewComment);
    }
    

    This action determines if the ModelState is valid before adding a comment to the database. If the ModelState is not valid, it is copied to TempData, which makes it available to the first action.

    // POST: /posts/comment
    [HttpPost, Authorize]
    public ActionResult Comment(CommentViewModel newComment)
    {
        if (!ModelState.IsValid)
        {
            TempData.Add(commentFormModelStateKey, ModelState);
            return Redirect(Url.ShowPost(newComment.PostID, newComment.Slug));
        }
    
        // Code to add a comment goes here.
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
I have a text area in my form which accepts all possible characters from
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.