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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:15:10+00:00 2026-06-17T15:15:10+00:00

I recently started using MVC, and as a test project I am creating a

  • 0

I recently started using MVC, and as a test project I am creating a simple “blog”. I have got a basic structure in place where the main page displays all the posts and when you click on it it will go into the detail page.

Structure

Now I am trying to add comments (Comment.cs)to my posts from within the Home/Details view, which basically requires me to have 2 models in 1 view. Model 1 is the Post model and Model 2 is the Comment model;

  • Post.cs is used to get the details of the post
  • Comment.cs is used to add a comment to the post

This is the code for my Home/details view:

@model MVCPortfolio.Models.Post

@{
    ViewBag.Title = "Details";
}

<h2>@Model.Title - @Model.Author</h2>

<fieldset>
    <legend>Posted on @Model.Date.ToShortDateString()</legend>
    <div class="content">
         @Model.Content
    </div>
</fieldset>
    <div class="comments">
        <ul>
            @foreach (var c in Model.Comments)
            {
            <li>
                @c.Content - @c.Author
            </li>
            }
        </ul>
    </div>
<div class="newcomment">
@*  @Html.EditorFor(model => model) *@
</div>

<p>
@* 
    @Html.ActionLink("New Comment", "Comment", new { id = Model.PostId }) 
*@
    |
    @Html.ActionLink("Back to List", "Index")
</p>

And this is my home controller, from within which I want to add comments.

    private PortfolioEntities db = new PortfolioEntities();

    //
    // GET: /Home/

    public ActionResult Index()
    {
        var posts = (from p in db.Posts
                     orderby p.Date
                     select p);

        return View(posts);
    }

    public ActionResult Details(int id)
    {
        var post = (from p in db.Posts
                    where p.PostId == id
                    select p).Single();

        return View(post);
    }

    [HttpPost]
    public ActionResult Comment(Comment comment)
    {
        if (ModelState.IsValid)
        {
            db.Comments.Add(comment);
            db.SaveChanges();

            return RedirectToAction("Details");
        }

        return View(comment);
    }
}

But what I don’t understand is how to add the comment to the post, I can easily add a new post (see my Create.cshtml view), but I cannot figure out how to add a comment from within the post detail view.

Thank you for your time,
Thomas

  • 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-17T15:15:12+00:00Added an answer on June 17, 2026 at 3:15 pm

    In view of the clarifications, I think you should create a partial view for that.

    In your view, subsitute

    @Html.ActionLink("New Comment", "Comment", new { id = Model.PostId }) 
    

    with a call to render a partial view

    @Html.Partial("_Comment")
    

    Your partial view should handle the UI for adding a Comment, ending with a call to your Comment action method in your PostController

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

Sidebar

Related Questions

So I have recently started using jQuery, jQueryUi and ASP.NET MVC. And I have
Ive recently started using Ruby on Rails for a project of mine and have
I've just recently started using VS2008 for a new tools project, and have been
I have recently started using ASP.Net MVC 3 RC 2 and have attempted to
I recently started using Git when creating a new project - until then I
I recently started looking into building web applications using .NET MVC and I stumbled
I've just recently started to build a web site using ASP.NET - MVC 4,
I recently started using rspec and factory_girl and I'm working on a basic control
I recently started using NERDTree with vim, and have set it to automatically open
Recently I've started to write on asp.net mvc framework. I have a problem. I

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.