Provide additional fields
If you want your post instance to be pre-populated as well, you will have to include its required fields as well.
I suspect you're having a post view where user has a form that displays comment fields so they can post a comment on the post.
suppose you have classes as:
public class Post
{
public int Id { get; set; }
[Required]
public string Title { get; set; }
public string Content { get; set; }
}
public class Comment
{
public int Id { get; set; }
[Required]
public string Author { get; set; }
[Required]
public string Content { get; set; }
public Post Post { get; set; }
}
Usual browser POST
What you would have to do is to add post fields (of hidden type) to your form. I don't know how you created your form whether you're using strong type views or not... Anyway. Your inputs would have to have names:
- Post.Id (for the sake of attaching new comment to a particular post
- Post.Title (because I've set it as required, but you could put in here whatever dummy text if all you'll be using is Post.Id; Title just needs to be set so your model validation won't fail)
- Author - visible (this is comment author)
- Content - visible (this is comment content)
Whether you're creating these fields using Html helpers or manually doesn't really matter. Default model binder will be able to create post object instance and your model validation won't fail.
Ajax POST
If you're posting your comments using Ajax you can do the same as described in the previous example and simply use jQuery's .serialize() function that will serialize form fields when calling $.ajax().
But you could as well programmaticaly collect all fields using Javascript and post that data to server just as well. You could simply create a JSON object:
var comment = {
Author: $("#Author").val(),
Content: $("#Content").val(),
Post: {
Id: $("#Post_Id").val(),
Title: $("#Post_Title").val()
}
};
$.ajax({
url: "someURL",
type: "POST",
data: $.toDictionary(comment),
success: function(data) {
// probably get a partial view with comment you can append it to comments
},
error: function(xhr, status, err) {
// process error
}
});
There are 2 things that are being used here and need some explanation:
Ajax error handling can also consume invalid model state errors (model validation failing) and can be accomplished this way.
Converting a complex (multi-level) JSON object has been done by using a special $.toDictionary() plugin that can be found here and provides the ability to simply consume complex JSON objects that are understood by Asp.net MVC default model binder. It wors with dates and lists as well.