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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T13:05:38+00:00 2026-06-06T13:05:38+00:00

I am trying to teach myself C# and MVC 3. I am learning by

  • 0

I am trying to teach myself C# and MVC 3.

I am learning by creating a simple blog application. I am stuck at detail page of the blog. This page displays the post and it works fine – that is, displays the post and the comments on the post. However, I want to add a form to add new comments.

I think the way to do that is to create a viewmodel which contains both blog and comment class and then I should be able to create a comment form which calls create action of comment controller.

However, when I do that the blog display page doesnt show up becasue it expects a model of type ‘BlogDetailViewModel’. I guess that is because the ‘Details’ action of ‘Blog’ controller is passing a ‘blog’ model to the view and not ‘BlogDetailViewModel’. How do I correct this error.

Is this the best way to deal with this issue.

I am detailing all the code below:

Blog Controller – Details Method

public ViewResult Details(int id)
{

    Blog blog = db.Blogs.Find(id);
    return View(blog);
}

Blog.cs

 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.cs

 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 int BlogID { get; set; } 
        public virtual Blog Blog { get; set; }
    }

BlogDetailViewModel.cs

public BlogDetailViewModel
{
    public Blog Blog{ get; set; }
    public Comment comment{ get; set; }
}

Blog Details View

@model NPLHBlog.ViewModels.BlogDetailViewModel

@{
    ViewBag.Title = @Model.Blog.Title;
}

….

I have tried to comment everything out of blog details view and just keep the title. However, even that doesn’t work.

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-06T13:05:40+00:00Added an answer on June 6, 2026 at 1:05 pm

    The type passed to View() in your controller must match the @model type in your view.

    public ViewResult Details(int id)
    {
        Blog blog = db.Blogs.Find(id);
        BlogDetailViewModel viewModel = new BlogDetailViewModel {Blog = blog, Comment = ""};
        return View(viewModel);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working with the NerdDinner application trying to teach myself ASP.NET MVC. However, I
I'm trying to teach myself .Net MVC 3, and am following this tutorial: http://www.asp.net/mvc/tutorials/getting-started-with-aspnet-mvc3/cs/intro-to-aspnet-mvc-3
I am trying to teach myself a bit of Javascript and made this collection
Hey guys, I'm new to UNIX, trying to teach myself and came across this
I'm just trying to teach myself how to use Linq. This is what I
I am trying to teach myself LRU algorithm using this youtube video . In
I have been trying to teach myself MEF, starting with this tutorial: http://blogs.msdn.com/b/brada/archive/2008/09/29/simple-introduction-to-composite-applications-with-the-managed-extensions-framework.aspx There
I'm trying to teach myself the basics of multiprocessing. As such, I found this
I am trying to teach myself WCF (using Learning WCF by Michele Leroux Bustamante),
I am trying to teach myself Smalltalk. A tutorial has this example of a

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.