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

  • Home
  • SEARCH
  • 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 5995177
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:55:22+00:00 2026-05-22T23:55:22+00:00

So i just started using ASP.NET MVC and i’m really liking it, except i

  • 0

So i just started using ASP.NET MVC and i’m really liking it, except i seem to have an odd knack to encounter the most bizarre of errors. I’m making a simple blogging application for myself. I have two simple models: post and comment. I have a partial view for creating a comment that is embedded in the details view for each post. When i submit the form to update the comment, it goes to my CommentsController’s create action, which looks like…

    [HttpPost]
    public ActionResult Create(comment comment)
    {
        comment.date = DateTime.Now;
        if (ModelState.IsValid)
        {
            post p = db.posts.Find(comment.post); //I've verified that comment.post is coming in
            if (p.comments == null) p.comments = new List<comment>();
            p.comments.Add(comment);
            db.Entry(p).State = EntityState.Modified; //I'm using this line since that's how its done in the edit actionmethod of the BlogController. I was just updating db.posts.Find(... manually, but that wasn't workign either.
            db.comments.Add(comment);
            db.SaveChanges();
            return RedirectToAction("Details", "Blog", new  { id = comment.post });
        }

        return PartialView(comment);
    }

The problem is that while the comment gets added to the database just fine, the post doesn’t update. When i examine p just before the changes are saved, it’s updated, but apparently it never actually commits to database since when i redirect to the Details, those comments aren’t there. Is there anything obviously wrong with my code? Am i missing some basic fundamental of .NET or MVC? Let me know if i need to provide more code or context.

Interesting Note: No matter what, post.comments always seems to be null. I set it to an empty list when the post is created, but it still seems to come back null. Not sure if this is just a result of trying to store an empty list or if it has to do with my problem, though. Again, lemme know and i’ll stick anything else needed up here.

Thanks!

  • 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-22T23:55:23+00:00Added an answer on May 22, 2026 at 11:55 pm

    Perhaps saving the changes is working fine but you don’t see the saved comments to a post because you don’t load them when you display the post. You can eager load the comments of a post in your action which displays a post like so:

    post p = db.posts
        .Include(p1 => p1.comments)
        .Where(p1 => p1.Id == id)
        .SingleOrDefault();
    

    I also think that you can simplify your Create action:

    [HttpPost]
    public ActionResult Create(comment comment)
    {
        comment.date = DateTime.Now;
        if (ModelState.IsValid)
        {
            db.comments.Add(comment);
            db.SaveChanges();
    
            return RedirectToAction("Details", "Blog", new  { id = comment.post });
        }
    
        return PartialView(comment);
    }
    

    This should work if comment.post is the foreign key of a comment to the related post. (Your code looks like this is the case, because of Find(comment.post))

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I’ve only just started using ASP.NET MVC, and I have a somewhat trivial question:
Just getting started using MVC in ASP.NET, I'm going to have it so users
I just got started running UI tests against my ASP.NET MVC application using WatiN.
I've just started using the MVP pattern in the large ASP.NET application that I'm
I'm an everyday C#/ASP.NET MVC/Visual Studio user and i've just started a project in
Or I don't understand this at all. I have started my ASP.NET MVC application
I just started using NLog in my asp.net application, and I need to see
Have just started using Google Chrome , and noticed in parts of our site,
Have just started using Visual Studio Professional's built-in unit testing features, which as I
I just started using the WPF WebBrowser that is included in Net 3.5 SP1.

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.