views:

102

answers:

1

I'm new in ASP.NET MVC2. But I want to develop a simply Newsletter site. The main problem which stuck me is how correctly use MVC pattern in News details and comments list.

For example:

I have a NewsController and action Details inside. And two model classes News and Comments. I want show all comments belongs to this News record and textarea for adding new comments. I wand display all this content in /News/Details/

How can i do it? I tried use PartialViews in Details View: one for Adding Comment and another for Comment list. I have passed comment object thorough ViewData["Comment"]. But i have problems with my models (think I cant use two models in View)

Again. How can I display Single Post + Comments + add comment view at single page using ASP.NET MVC2?

Another Example is stackoverflow.com. Here is an question + answers + write answer. I need same structure

+1  A: 

@ck3g, don't worry it's actually easier than you think.

First of all yes you can pass two models to a view. You simply need to wrap them up into third class and pass the third class back to the view;

  • class A() {}
  • class B() {}
  • Class C() { public A a; public B b }

Pass class C to your view.

If class B was all your comments then I would have a partial view that took B as its model. I would then do a foreach on the model and call another partial view to render the individual comment. this makes styling and refactoring easier later on.

I would also wrap the partial view in it's own div so that you can replace the contents later on after an ajax call.

To add a comment i would again have a partial view and when you press the submit button I would do a jquery call to an actionresult, save the comment and then re-read the entire comments from the database. then, in your c# code, return a partial view back to the jquery call using RenderPartial("partialviewname", commentsModel);.

once you are back in javascript land you can replace the entire contents of the comments parent div you created earlier with the new html and there you have comments.

What you may want to do now is research this in NerdDinner and then ask more specific questions.

Good luck and have fun.

griegs