Accessing Linq Values in ViewData

Posted by Jemes on Stack Overflow See other posts from Stack Overflow or by Jemes
Published on 2010-03-19T12:33:40Z Indexed on 2010/03/19 14:51 UTC
Read the original article Hit count: 412

Filed under:
|

I'm having trouble accessing the id, area and theme values in my ViewData.

They are being set in my action filter but when I get to the Site.Master I don't have access to them.

Any help or advice would be great.

ActionFilter

public override void OnActionExecuting(ActionExecutingContext filterContext)
{
    int SectionID = Convert.ToInt32(filterContext.RouteData.Values["Section_ID"]);
    int CourseID = Convert.ToInt32(filterContext.RouteData.Values["Course_ID"]);

        if (CourseID == 0)
        {
            filterContext.Controller.ViewData["Styles"] = (from m in _dataContext.Styles where m.Area_ID == SectionID select new {theme = m.Area_FolderName }).ToList();
        }
        else
        {
            filterContext.Controller.ViewData["Styles"] = (from m in _dataContext.Styles where m.Course_ID == CourseID select new { theme = m.Course_FolderName }).ToList();
        }
    }
}

Site.Master

<%@ Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage" %> <%@ Import Namespace="Website.Models" %>

<%       
    foreach (var c in (IEnumerable<Styles>)ViewData["Styles"])
   {
    Response.Write(c.Theme);
}%>

© Stack Overflow or respective owner

Related posts about asp.net-mvc

Related posts about LINQ