4

I'm new to ASP.NET MVC and I'm trying to create a very simple blog type site as a means of learning how everything works. But, I'm having a problem when posting from a comment form to a model which is null and I can't tell why.

On a blog post page, I have an "add comment" link which calls some JQuery to render a partial view that is strongly typed to the CommentModel. The link passes in the ID of the blog post as well and the partial is coded like:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Blog.Models.CommentModel>" %>

<% using (Html.BeginForm())
   { %>

    <%: Html.HiddenFor(x => x.Post.ID) %>
    <%: Html.HiddenFor(x => x.CommentID) %>

    <%: Html.TextBoxFor(x => x.Name) %><br />
    <%: Html.TextBoxFor(x => x.Email) %><br />
    <%: Html.TextBoxFor(x => x.Website) %><br />
    <%: Html.TextAreaFor(x => x.Comment) %><br />

    <input type="submit" value="submit" />

<% } %>

The CommentsModel is simple and looks like this (I haven't applied any validation or anything yet):

public class CommentModel
    {
        public BlogPost Post { get; set; }
        public int CommentID { get; set; }

        public string Name { get; set; }
        public string Email { get; set; }
        public string Website { get; set; }
        public string Comment { get; set; }
    }

This is then supposed to post to a simple controller action which will add the comment to the database and return the user to the page. For the sake of simplicity, I've stripped out most of the code but it looks similar to:

[HttpPost]
        public ActionResult CommentForm(CommentModel model)
        {
            if (ModelState.IsValid)
            {

            }
            else
            {

            }
        }

Everything works as expected, except that when posting the comment form, the comment model is null. I can't figure out why this is null. When I view the source of the rendered partial view, I can see that the "Post.ID" is populated with the correct ID, but this is lost when the form is submitted.

Am I missing something obvious here? I've set up forms similar to this in the past and it's worked fine, I can't understand why its not now. Thanks in advance.

Later Edit:

I had typed the code incorrectly and changed the public ActionResult CommentForm(CommentModel model) from public ActionResult CommentForm(CommentModel comment) which was causing the problem.

Thanks for the help.

NickK
  • 45
  • 1
  • 1
  • 5

2 Answers2

6

Similar kind of question has been answered yesterday. Check out : MVC3 - Insert using ViewModel - Object reference not set to an instance of an object

The problem I can see is , when the form is posted, the Post.ID and CommentID are passed, whereas your action is expecting a full blown object of type "CommentModel". The model binder is unable to map the post data, into the corresponding model object.

Community
  • 1
  • 1
Pawan Mishra
  • 7,212
  • 5
  • 29
  • 39
  • 1
    Thank you for posting this, I was able to solve the problem from that answer. I double checked my code and the issue was I had the controller post set up as (CommentsModel comment) - which wasn't working... Silly mistake! Thanks for the help. – NickK Nov 06 '11 at 12:30
0

Add:

public int PostID {get; set;}

...to your model, and populate that in your controller as a hidden input. The Post object is not going to parse easily.

Jeremy Holovacs
  • 22,480
  • 33
  • 117
  • 254