views:

153

answers:

2

Hi Guys, I am currently considering the best way of building a relatively complex page in asp.net mvc. The page (and pages like it) will contain numerous 'controls' such as a shopping basket, recent news widget, login controls and more. In other words, it will be very component based.

My question is, what is the best way of building something like this in asp.net MVC? In regular webforms the answer would be simple thanks to user controls and the fact they can be nicely self contained. In MVC, I understand that in theory I should probably build a viewmodel that includes all the data needed for all my widgets and then render partial views in whatever page I'm building. Could an alternative approach be to use javascript to load widgets 'dynamically' (think jQuery load) by simply calling into controllers that render partial views. That way I could have a basket controller, which when called renders out a basket. Of course, this relies on javascript....

Whats the best practise for situations like this?

Thanks

+1  A: 

You could of course use JavaScript to populate the page sections but then this content will not be accessible to search engines (but that probably does not concern you).

What you need to understand that there is currently no way to make these partial views perform independently. Like talking to their own controller and posting data while having the rest of the stay unchanged (like it could have been done with WebForms and user controls). If you think postbacks and control state these things do not exist any longer. Your "control" makes a post, the controller has to process the request and then recreate the complete view along with all element values, states and other "user controls".

Unless you do it asynchronously with JavaScript of course. But then it's not gonna be an accessible page any more.

Developer Art
A: 

You can try SubControllers from MvcContrib or Steve Sanderson`s "Partial Request" approach.

But i warn you - communication between (partial requests, i haven't tried subcontrollers myself) them might get tricky and lead to mega failure. Use them only if you are completely sure that they are completely independent.

Anyway - that's a bad idea and should be avoided through controller/viewmodel inheritance or something....

Arnis L.
I think that post by steven sanderson pretty much clears the issue up for me. His solution seems neat. I wonder if MS will include any official mechanism for this kind of functionality outside of asp.net mvc futures
Sergio
No ideas if MS will include anything - probably not, because it's kind a contradictory with MVC pattern. And believe me - that solution is tricky. Communication between those 'widgets' will kill you. :)
Arnis L.