开发者

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:

  1. ASP.NET MVC - Set ViewData for masterpage in base controller
  2. http://www.asp.net/mvc/tutorials/passing-data-to-view-master-pages-cs
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜