views:

46

answers:

4

I am writing a basic blogging application in ASP.NET MVC. I have a Post model which can be displayed via View.aspx. On that page I also want to have a form to submit a comment, but as View.aspx inherits the Post model, I don't know how to have it edit a Comment model.

+1  A: 

In your controller you can use whatever model you like. So place a form in your view which points to Post/Comment/{postid} or Comment/Add/{postid}. After you added the model to the database redirect to the previous view.

Neothor
+1  A: 

tsv - create a few partialviews for the comments (strongly typed) and create a couple of methods on the comment controller:

  • GetComments(int postId)
  • AddComment(Comment newComment)

in your post view, call the partialview either via $ajax or directly in the page.

jim

jim
A: 

As Jim said, you can create the necessary methods in a different or in the same Controller and call <% Html.RenderAction("methodName","ControllerName") %> in your view.aspx which will still inherit the Post Model. Then, create usercontrols for the form to submit a comment and to display all the comments.

Shruthi
+1  A: 

The key thing to understand here is when your page 'inherits' a class (Post, in your case), this is purely so that the Model does not need to be cast in any way. This does not restrict you from referring to other classes within the aspx page, nor does it prevent the page from having a form which posts to another Controller/Action that inherits from a completely different class (Comment, in your case).

Maybe if you posted some example code, we could suggest ways to make it work the way that you want.

belugabob
Thanks, that has made it a lot clearer to me, did misunderstand the concept a bit there.
tsv