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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T08:43:03+00:00 2026-06-01T08:43:03+00:00

I have the following View, Controller and Comment.cs code, but I get an error,

  • 0

I have the following View, Controller and Comment.cs code, but I get an error,

The model item passed into the dictionary is of type ‘TMPBlog.Models.Comment’, but this dictionary requires a model item of type ‘TMPBlog.Models.Post’.

If I don’t enter any text into the two required text fields @Html.TextBox(“CommentEmail”) and @Html.TextArea(“CommentDetail”, new { cols = “65”, rows = “7” }), if I do it works fine.

Controller code,

public ActionResult Index(int PostID)
{
    Post post = db.Posts.Single(p => p.PostID == PostID);
    return View(post);
}

[HttpPost]
public ActionResult Index(Comment comment)
{
    if (ModelState.IsValid)
    {
        string CommentEmail = comment.CommentEmail;

        string CommentDetail = comment.CommentDetail;
        CommentDetail = CommentDetail.Replace("\n", "<br />");
        comment.CommentDetail = CommentDetail;

        db.Comments.AddObject(comment);
        db.SaveChanges();

        return RedirectToAction("CommentResponse", new { id = comment.CommentID });
    }
    return View(comment);
}

Comment.cs,

using System.ComponentModel;
using System.ComponentModel.DataAnnotations;

namespace TMPBlog.Models
{
    [MetadataType(typeof(CommentMetaData))]
    public partial class Comment
    {
    }

    public class CommentMetaData
    {
        [Required(ErrorMessage = "You must enter an email address!")]
        public object CommentEmail { get; set; }

        [Required]
        public object CommentDetail { get; set; }
    }
}

View,

@model TMPBlog.Models.Post

@Html.ValidationSummary("There is an error")

@using (Html.BeginForm())
{

    @Html.Hidden("CommentDate", String.Format("{0:yyyy-MM-dd HH:mm}", DateTime.Now))
    @Html.Hidden("PostCommentFK", @Html.DisplayFor(model => model.PostID))

    <br />
    <span class="BlueHeading">Add Your Comments Here!</span>
    <br /><br />

    <div class="editor-label">
        Email Address
    </div>
    <div class="editor-field">
        @Html.TextBox("CommentEmail")
        @Html.ValidationMessage("CommentEmail", "*")
    </div>

    <div class="editor-label">
        Comments
    </div>
    <div class="editor-field">
        @Html.TextArea("CommentDetail", new { cols = "65", rows = "7" })
        @Html.ValidationMessage("CommentDetail", "*")
    </div>

    <div class="editor-field">
        @Html.CheckBox("CommentTicked") Email me when others comment
    </div>
    <p>
        <input type="submit" value="Add Comment" />
    </p>
}

How do I solve this?

Cheers,

Mike.

  • 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-01T08:43:04+00:00Added an answer on June 1, 2026 at 8:43 am

    Your view is strongly typed to TMPBlog.Models.Post, but in your POST controller action you are passing a comment:

    return View(comment);
    

    This obviously is wrong.

    You could simply return the same view:

    return View();
    

    Since everything is stored in the modelstate the view will be correctly redisplayed with the corresponding error messages.

    Also the following line:

    @Html.Hidden("PostCommentFK", @Html.DisplayFor(model => model.PostID))
    

    could probably be simplified:

    @Html.Hidden("PostCommentFK", Model.PostID)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following model, view and controller. Model public class Person { public
I am making a color picker. I have the following code: View controller.h: @interface
I have the following code in a view controller that (in all other respects)
I have the following view controller in my UIPopover: @protocol StorePopoverDelegate - (void)storeSelected:(NSString *)store;
I have the following ActionLink in my view <%= Html.ActionLink(LinkText, Action, Controller); %> and
If i have a controller action Create that returns a view with the following
I have a viewcontroller like the following. But the touchsBegan doestnt get detected. Can
I have the following view: @model MyModel1 @{ ViewBag.Title = Index; Layout = ~/Views/Shared/_Layout.cshtml;
I have the following line in my view controller: -(void) retrieveAccounts { accounts =
I'm having some trouble getting a view to flip. I have the following code

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.