I am building a pretty big MVC project, and I'm having some trouble with my view data. I have a pretty complicated model structure. I didn't want to 'polute' my models with simple display data, but I wanted strongly typed data, so I wrote a couple of these simple classes:
public class GeneralViewData
{
private static string Id = "GeneralViewData";
public string PageTitle { get; set; }
public bool ShowGuestMenu { get; set; }
public string CurrentUserName { get; set; }
public bool ShowOrganisationAdminMenu { get; set; }
protected GeneralViewData() { }
public static GeneralViewData Retrieve(ViewDataDictionary viewDataDictionary)
{
object viewData = viewDataDictionary[Id];
if (viewData == null) return Initiate(viewDataDictionary);
return (GeneralViewData)viewData;
}
private static GeneralViewData Initiate(ViewDataDictionary viewDataDictionary)
{
GeneralViewData viewData = new GeneralViewData();
viewDataDictionary[Id] = viewData;
return viewData;
}
}
So anywhere you can access the ViewData, you can easily get the model and access it with:
var generalViewData = GeneralViewData.Retrieve(ViewData);
This works perfectly for most of the site, but I'm having trouble with the views of MVCSiteMapProvider. Aparently the sitemapprovider does not pass the main ViewData, but instead passes a new one to the partial views.
I need a way to either make MVCSiteMapProvider pass the full ViewData to its views, or a complete way around this by maybe storing the GeneralViewData instance somewhere else. I've been racking my brain for a way around this, so I'm open for anything.