How to bind form collection back to custom model object that uses 2 custom objects in asp.net mvc?

Posted by baijajusav on Stack Overflow See other posts from Stack Overflow or by baijajusav
Published on 2010-03-23T02:50:11Z Indexed on 2010/03/23 3:01 UTC
Read the original article Hit count: 480

Filed under:
|

What I'm trying to do is rather basic, but I might have my facts mixed up. I have a details page that has a custom class as it's Model. The custom class uses 2 custom objects with yet another custom object a property of one of the 2. The details page outputs a fair amount of information, but allows the user to post a comment. When the user clicks the post button, the page gets posted to a Details action that looks something like this:

        [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Details(VideoDetailModel vidAndComment) { ....}

The only fields on the form that is posted are CommentText and VideoId. Here is what the VideoDetailModel looks like.

public class VideoDetailModel
{
    public VideoDetailModel()
    {
        Video = new VideoDTO();
        Comment = new CommentDTO();
    }

    public VideoDetailModel(VideoDTO vid)
    {
        Video = vid;
        Comment = new CommentDTO();
    }

    public VideoDTO Video { get; set; }
    public CommentDTO Comment { get; set; }
}

VideoDTO has a few properties, but the ones I need are VideoId. CommentDTO's pertinent properties include CommentText (which is posting correctly) and a UserDTO object that contains a userId property. Everything other than the CommentText value is not being posted. I also have the following line on the ascx page, but the model value never gets posted to the controller.

Html.Hidden("Model.Video.VideoId", Model.Video.VideoId);       

I'm really not sure what I'm missing here. I suppose if I added more form fields for the properties I need, they would get posted, but I only need 1 form entry field for the CommentText. If I could get the same Model objects value that were sent to the page to post with the page, that would help.

I'll be happy to make any clarifications needed here. I'm just at loss as to what's going on.

© Stack Overflow or respective owner

Related posts about asp.net-mvc

Related posts about ASP.NET