Where would you store data, and possibly methods, that is to be used on all views in MVC3? For example, admin email address and site title.
This was accomplished in aspx pages by putting properties and methods on base page that all other pages extended from. Should I do something similar with a base controller class, using the base page class constructor to prepopulate the viewbag? Or should
I use the approach of creating a base view model?
bluesherpa
Member
130 Points
48 Posts
Where to store data common to all views?
Feb 23, 2012 12:26 AM|LINK
Where would you store data, and possibly methods, that is to be used on all views in MVC3? For example, admin email address and site title.
This was accomplished in aspx pages by putting properties and methods on base page that all other pages extended from. Should I do something similar with a base controller class, using the base page class constructor to prepopulate the viewbag? Or should I use the approach of creating a base view model?
Thanks,
Ed
bruce (sqlwo...
All-Star
36836 Points
5443 Posts
Re: Where to store data common to all views?
Feb 23, 2012 03:42 AM|LINK
a base controller.
ignatandrei
All-Star
134947 Points
21626 Posts
Moderator
MVP
Re: Where to store data common to all views?
Feb 23, 2012 07:05 AM|LINK
In cache ( maybe application)
bluesherpa
Member
130 Points
48 Posts
Re: Where to store data common to all views?
Feb 26, 2012 07:28 PM|LINK
Thanks for the feedback =)
jerryjoseph
Contributor
6740 Points
1257 Posts
Re: Where to store data common to all views?
Feb 26, 2012 09:09 PM|LINK
It depends on the kind of data.
I would store Admin Email Address in a config file.
I would have Page Title as a property of BaseViewModel and if it is the same for all pages I will set it in BaseController.
linkedin | twitter | www.jerryjoseph.net
bluesherpa
Member
130 Points
48 Posts
Re: Where to store data common to all views?
Feb 26, 2012 10:24 PM|LINK
Good points. I'll try that and let you know how it turns out.
bluesherpa
Member
130 Points
48 Posts
Re: Where to store data common to all views?
Feb 27, 2012 01:20 AM|LINK
Doesn't this approach end up with having to create a custom view model for every action?
jerryjoseph
Contributor
6740 Points
1257 Posts
Re: Where to store data common to all views?
Feb 27, 2012 08:00 AM|LINK
Not exactly.
Use a ViewModel per View.
linkedin | twitter | www.jerryjoseph.net
bluesherpa
Member
130 Points
48 Posts
Re: Where to store data common to all views?
Feb 27, 2012 04:59 PM|LINK
Where do you put your view specific models? Do you just put them in the ~/models directory or do you separate them out to their own directory?
jerryjoseph
Contributor
6740 Points
1257 Posts
Re: Where to store data common to all views?
Feb 27, 2012 05:32 PM|LINK
A good way is to create a folder called ViewModels inside the Models folder.
Take a look at this. http://geekswithblogs.net/dotNETvinz/archive/2011/06/03/asp.net-mvc-3-creating-a-simple-sign-up-form.aspx
linkedin | twitter | www.jerryjoseph.net