I’ve been working on my first major ASP.NET MVC application (plus lots of jQuery) for a month or so now and I’m pretty happy with it. I have a page that looks something like this. (Actual data changed to protected the innocent.)
<div id="main"></div>
<div id="movies"></div>
<div id="actors"></div>
<div id="actor-ratings"></div>
<div id="ratings-comments"></div>
The movies div contains a table of movies to select from. The actors, actor-ratings, and ratings-comments divs are all hidden at this point. When a movie is clicked, I have a jQuery live() event to get the click, post to the Action, get the data, and then a callback function to take the data and place it in a modal pop-up. (I think I have that order right, feel free to tell me if I’m doing it wrong anywhere.)
Since actors, actor-ratings, and ratings-comments all potentially need to be loaded in one page, my controller ends up having a huge action to load all that data in. Plus, the ratings-comments div may close the actor-ratings modal and open itself up in a new modal. And on top of it I want to implement the jQuery Address plugin to allow the back button to go back to previously opened modals.
Gets kind of complex pretty quick. Should I be using partial views for each of those divs? Should I have one controller with actions for each of those? Or a controller for each partial view/div? Partial divs seems like the way to go, but I guess I’m looking for some guidance before I jump in and break up my code. Any advice would be appreciated.
Edit: Also, my actor-ratings partial view would probably need some of the data from the actors partial view. Do I just have my ActorRatings controller action load in the actor data too? (I’m passing strongly-typed view models to the view.)
How you break up your code doesn’t necessarily matter too much so long as it consistently separates logically different parts of the application.
For a page which is driven by lots of partial views which are closely related I would be tempted to put the action methods in a single controller. However, if you’re looking to make a very rich AJAX-driven page then you might benefit from splitting disparate functionality into separate controllers.
Returning a partial view from the action methods is probably the most maintainable and flexible solution but returning JSON would use the least bandwidth. For what you’ve described I would be tempted to return partial views.
It’s difficult to comment on the shared data between Actors and ActorRatings without more detail. Can’t the Actors action method get all the detail it needs or is it updating due to the ActorRatings method being called?