How to pass a variable from Controller Initialization to a Master Page in MVC2
in a controller:
protected override void Initialize(RequestContext requestContext)
{
if (db == null) { db = new myRepository(); }
if (currentCalendar == null)
{
currentCalendar =
db.FindCalendarInfoByUrl(
requestContext.RouteData.Values["calurl"].ToString());
// if not found ... show Unavailable Content View
if (currentCalendar == null)
RedirectToAction("ContentNotAvailable");
// to show in the MasterPage
Session["GoogleId"] = currentCalendar.GoogleAnalyticsId;
}
base.Initialize(requestContext);
}
Session
variable is not yet defined in this time of the request, and I can't use ViewData
either as It's always null...
How can I use to pass this 2 variables GoogleId
and CalendarUrl
to the MasterPage
?
I'm thinking that I could use Cache
, but there are so many and I'm not yet into Caching:
alt text http://www.balexand开发者_C百科re.com/temp/2010-11-17_2328.png
What is my best option to accomplish this?
I'm not sure why your ViewData is null in the Master Page, but try overriding the OnActionExecuting
event.
protected override void OnActionExecuting(ActionExecutingContext filterContext)
Also take a look at the following:
- ASP.NET MVC - Set ViewData for masterpage in base controller
- http://www.asp.net/mvc/tutorials/passing-data-to-view-master-pages-cs
精彩评论