Paul Palermo
Paul Palermo

Reputation: 1

MVC Models for complex page

I'm working on an ASP.Net MVC project. My index page will be similar to facebook's which means that the user can write a message but also sees the messages of his/her friends and a list of his friends is shown too. That means that there are two outputs and one input.

How should my Models for this page look like? Is it a good idea to have one IndexModel containing a list of all messages (List), a list of all friends (List), and an InputMessage class? Or should I write one Model for each of them and put them together within a ViewModel?

Thanks

Upvotes: 0

Views: 171

Answers (3)

Muhammad Adeel Zahid
Muhammad Adeel Zahid

Reputation: 17784

Friends and Messages are two different concerns so they got to be in different ActionResults, no matter how you plan to display them later on (using templating or something else)

Upvotes: 0

Pawan Mishra
Pawan Mishra

Reputation: 7268

If I am correct then your webpage will have static(list of friends) as well as dynamic(list of messages) content. I would suggest you to have a strongly typed view with with your model containing all the static content including the list of friends e.g. IEnumerable.

For messages create partail view using jQuery-template feature. Define the template as on how to display the messages, bind the template with raw json data(which will basically contain your messages) and embed this partial view in you strongly typed view.

Partial views can be resused so tomorrow you can use the same view to show messages else where in application.

For more on how to design using jQuery template : https://github.com/nje/jquery-tmpl/wiki/List-of-jQuery-tmpl-articles-and-tutorials

Upvotes: 0

Vadim
Vadim

Reputation: 17957

Your best bet is actually to split out either the friends list, messages list or both into their own partial views. Then if you don't want to have one controller action generate data for them, you can create actions for each of them and use Html.RenderAction to show them.

http://msdn.microsoft.com/en-us/library/system.web.mvc.html.childactionextensions.renderaction.aspx

Upvotes: 1

Related Questions