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 8168097
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T20:24:33+00:00 2026-06-06T20:24:33+00:00

I am trying to create a simple blog application using asp.net. I am a

  • 0

I am trying to create a simple blog application using asp.net. I am a novice and trying to teach myself c# and asp .net mvc.

The problem that I am facing is that when I try to add a comment to a post then it doesn’t work. I do get a comment attached to the post but it doesn’t display anything. Also, when I check the database – it just contains Null fields except for the BlogID field. What am I doing wrong?

My code is below:

Blog – class

public class Blog
{
    public int BlogID { get; set; }
    public string Title { get; set; }
    public string Writer { get; set; }

    [DataType(DataType.MultilineText)]
    public string Excerpt { get; set; }

    [DataType(DataType.MultilineText)]
    public string Content { get; set; }

    [DataType(DataType.Date)]
    public DateTime PublishDate { get; set; }

    public virtual ICollection<Comment> Comments { get; set; }
}

Comment – class

public class Comment
    {
        public int CommentID { get; set; }
        public string Name { get; set; }

        [DataType(DataType.EmailAddress)]
        public string Email { get; set; }

        [DataType(DataType.MultilineText)]
        public string CommentBody { get; set; }

        public virtual int BlogID { get; set; } 
        public virtual Blog Blog { get; set; }
    }

BlogDetailViewModel

public class BlogDetailsViewModel
{
    public Blog Blog { get; set; }
    public Comment Comment { get; set; }
}

Blog – Details controller

public ViewResult Details(int id)
{

    Blog blog = db.Blogs.Find(id);
    BlogDetailsViewModel viewModel = new BlogDetailsViewModel {Blog = blog};
    return View(viewModel);
}

Blog Details – view

@model NPLHBlog.ViewModels.BlogDetailsViewModel

has code to display blogpost + comment form at end of blog details page below:

   @using (Html.BeginForm("Create", "Comment"))
    {
        <input type="hidden" name="BlogID" value="@Model.Blog.BlogID" />
        <div class="editor-label">
            @Html.LabelFor(model => model.Comment.Name)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Comment.Name)
            @Html.ValidationMessageFor(model => model.Comment.Name)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Comment.Email)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Comment.Email)
            @Html.ValidationMessageFor(model => model.Comment.Email)
        </div>            

        <div class="editor-label">
            @Html.LabelFor(model => model.Comment.CommentBody)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Comment.CommentBody)
            @Html.ValidationMessageFor(model => model.Comment.CommentBody)
        </div>
        <p>
            <input type="submit" value="Add Comment" />
        </p>

comment – create controller

public class CommentController : Controller
{
    private NPLHBlogDb db = new NPLHBlogDb();

    //
    // GET: /Comment/

    public ActionResult Create(Comment c)
    {
        Blog blog = db.Blogs.Single(b => b.BlogID == c.BlogID);
        blog.Comments.Add(c);
        db.SaveChanges();
        return RedirectToAction("Details", "Blog", new { ID = c.BlogID });
    }

Any help would be grateful.

  • 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-06-06T20:24:34+00:00Added an answer on June 6, 2026 at 8:24 pm

    Found the answer … I updated the create action of comment controller to:

    public ActionResult Create(BlogDetailsViewModel viewModel)
    {
        Blog blog = db.Blogs.Single(b => b.BlogID == viewModel.Comment.BlogID);
        Comment c = viewModel.Comment;
        blog.Comments.Add(c);
        db.SaveChanges();
        return RedirectToAction("Details", "Blog", new { ID = c.BlogID });
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am new to ASP.NET-MVC and I am trying to create a simple blog
I'm new to ASP.NET MVC and I'm trying to create a very simple blog
I'm trying to create a demo website using websockets that will be a simple
I am trying to create a simple blog that has code inclosed in <pre>
So I am teaching myself rails and I am trying to create simple blog
I'm trying to create a simple global exception handler in a WPF application that
I'm testing CodeIgniter, and trying to create a simple blog. The video tutorial on
I'm trying to create simple script that subscribes a user to a company calendar.
Im trying to create a simple pan and zoom app using silverlight 4, but
I am trying to create a simple about page that uses JQuery click to

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.