views:

31

answers:

1

In my project I have strongly typed viewdata in the spirit of the following:

AppleListViewData : ViewDataDictionary
  + SelectedTheme
  + ThemeList
  + SelectedApple
  + AppleList

Where the first four are re-occuring accross all the viewdatas because they are used in the site.master
To keep it DRY I elevated them into a BaseViewData class which the other inherit from.

Now all my controllers need to fill up these properties. Again, to keep it DRY I created a BaseController for my controllers that takes on this job.

BaseController : Controller
  + new ViewData : BaseViewData
  + FetchThemes
  + FetchSelectedTheme

The BaseController.ViewData is newed because it hides the Controller.ViewData All the controllers are responsible of newing up the viewdata at constructor time.

I'm essentially hiding the existing viewdata system and replacing it with my own.
This is where I feel I'm doing something wrong. Any suggestions how I can make this work in a more elegant way?

+2  A: 

If these items are always in your master pages, it may make sense to extract the controls which use the data and have a controller handle each one. Then in your masterpage use Html.RenderAction and output the user control with your data. This way none of your controllers get cluttered with information about the theme views, and anyone reading your code will easily see where the data comes from

Example

public class ThemeController : Controller{
  public ActionResult ThemeDropDown(){
     return PartialView(new ThemeViewModel(){ SelectedTheme = ..., ThemeList = ... })
  }
}


public class AppleController : Controller{
  public ActionResult AppleStuff(){
     return PartialView(new AppleViewModel(){ Apple = ..., AppleList = ... })
  }
}


<%= Html.RenderAction("ThemeDropDown", "Theme") %>    
<%= Html.RenderAction("AppleStuff", "Apple") %>
amarsuperstar
This could be a nice alternative to my system indeed. Gonna see if I can do this in my real app :)
borisCallens