tags:

views:

93

answers:

2

Take the following hypothetical situation, how would I implement it in MVC?

All my pages sit in a Master, which has a CurrentUser property on it. This object is determined from some session user id, and then queried from the db on every page load. This object is then used throughout the site, for example to display "Welcome _" at the top, and used in various page codebehinds as part of database queries etc. One page has several user controls on it, each one displaying different info, queried on first load. When one control posts back and alters it's display somehow, the rest of the page remains the same without needing repopulating.

I get the basics of mvc, however I'm strugging to understand how to take it to the next (real world) level of content rich views. How do you expose common objects to views? Does the controller HAVE to stick every bit of data a view could possibly need into the ViewData?

Can anyone recommend a good tutorial on more advanced mvc usage, which will help me understand how to move away from webforms?

+1  A: 

Hi

Check out Stephen Walthers blog post about passing data to masterpages and User Controls

http://weblogs.asp.net/stephenwalther/archive/2008/08/12/asp-net-mvc-tip-31-passing-data-to-master-pages-and-user-controls.aspx

TT
+2  A: 

I prefer to place all the data needed by my views into the ViewData. I try to use a strongly typed model most of the time and I've had some success in adding common properties used throughout my application to a common BaseViewData class.

For common properties like user information I've let the BaseViewData class be responsible for populating or providing those property values so that the each controller doesn't have to worry context information that isn't necessarily the concern of a specific action.

I'm interested to see how others have solved the problem as well.

Andrew Hanson