views:

191

answers:

2

All my controllers are based off of a BaseController, to share properties between them and override OnActionExecuting to set some values based on the route.

I'm creating a BaseViewData class to do the same for all my view data.

At the moment I'm populating the view data like so (C#):

var viewData = new BaseViewData
{
    Name = "someName",
    Language = "aLanguage",
    Category = "aCategoryName"
};

I do this in every action that requires the view data. Some of the properties are common, need to be set throughout every action. Is there a way to set some of the properties on a more global scale?

If I instantiate the BaseViewData class in the OnActionExecuting method on the BaseController, how do I access the BaseViewData properties from the action in the regular controllers (derived from the BaseController)?

Update in response to Dennis Palmer:

I'm essentially doing this because of a nagging issue I'm having with ViewData["lang"] not being populated randomly on some requests. ViewData["lang"] contains "en" if the language is English, and "ja" if it is Japanese (well, it's supposed to anyway). I populate ViewData["lang"] inside OnActionExecuting on the BaseController.

In my view, I make a call to some partial views based on the language:

<% Html.RenderPartial(ViewData["lang"] + "/SiteMenu"); %>

But I'm randomly getting errors thrown that state "Cannot find /SiteMenu", which points to the fact that ViewData["lang"] has no value. I just cannot find any reason why ViewData["lang"] would not get populated. So, I'm rewriting the site to use ONLY strongly typed view data (and setting some hard defaults). But if another method is better, I'll go that way.

Thank you!

+1  A: 

Perhaps instead of this inheritance scheme you have, you can just use action filters to add the data you need.

Frank Krueger
Hmm... possibly. Thank you for the link - first time I'd seen it and have always been meaning to look into action filters more.
Chad
+1  A: 

I'm not sure I follow exactly what you're trying to do, but if your view is using values in the route to display certain information, it seems like adding your own extension methods for HtmlHelper would be a better way to go.

Are Name, Language and Category contained in your routes? If so, then HtmlHelper will have access to the route info and can determine what to display via the extension methods. What is the correlation between your routes and what your views need to know?

Update: Is lang part of your route? If so, then I would still contend that you could write an HtmlHelper extension method that looks at the route data directly and determines which partial view to render. That way your controller wouldn't even need to worry about setting the ViewData["lang"]. The view would always know how to render based on the route.

Update 2: I think dismissing use of an HtmlHelper extension method because it re-evaluates the route data might be a case of premature optimization. Your controller inheritance scheme sounds overly complex and you asked the question because the way you were setting ViewData was unreliable. I doubt that pulling the value from route data would be much, if any, less efficient than setting and reading from ViewData.

From your comment:

In the controller I use the lang value to determine which view to show as well.

That only makes me think that there are more pieces of your system that I'd need to see in order to give better advice. If you have separate views for each language then why does the view need to be told which language to use?

Another alternative to consider would be using nested master pages. You could have a single master page for your site layout and then a nested master page for each language that just contains a hard coded lang value.

Dennis Palmer
Yes, lang is part of my route. In the controller I use the lang value to determine which view to show as well. For example, www.apoads.com/en/Yokota. The route looks like: "{language}/{b}" (b is short for base, in this case military base - not related to programming).
Chad
I built an html helper to grab the route data and return the language based on the route token. My master page makes many references to this value... using an HtmlHelper extension means it's doing the work to get this value every single time it is referenced, correct? That's one reason why I was setting it on the controller, do the work once and reference that value multiple times. Or am I mistaken?
Chad
All very good advice. In my case, there is one master page that pulls a couple of different partial views based on language. Inside the other views, yes, the language is hard coded. What I'm doing now is building a BaseViewData class, making it a property of the BaseController and instantiating it with "en" set at the language by default. Since EVERY view will need access to a few key pieces of data, I went this route. I do still have the HtmlHelper written that you suggested, in case it ever becomes handy. I thank you for your responses.
Chad