0

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);
    }%>
Jemes
  • 407
  • 1
  • 10
  • 27
  • If you add your reference to your Website.Models namespace to the namespaces section of your web.config you will not need to have the declaration in your page as it will be registered globally. – Nicholas Murray Mar 19 '10 at 15:23

1 Answers1

3

Editting again based on your feedback...

I think you'd best be served by creating a ViewModel so that you can strongly type your View.

Create a class like the following (you can add fields as needed):

public class StyleViewModel
{
    public string Id {get; set;}
    public string Area {get; set;}
    public string Theme {get; set;}
}

Then in your controller:

filterContext.Controller.ViewData["Styles"] = 
    (from m in _dataContext.Styles 
    where m.Area_ID == SectionID
    select new StyleViewModel
    {
        Id = m.Area_ID
        Area = m.Area_Name
        Theme = m.Area_FolderName
    }).ToList();

You can then clean up the code in your View:

<% 
    foreach (var c in Model)
    {
        Response.Write(c.Theme);
    }
%>
Justin Niessner
  • 242,243
  • 40
  • 408
  • 536
  • Thanks for the quick response. I'm still not getting access to the values in Site.Master though? I get intellisense for my database names but not for the Anonymous Types? – Jemes Mar 19 '10 at 12:42
  • Do you need to use an Anonymous Type? – Justin Niessner Mar 19 '10 at 12:48
  • I do need to use Anonymous Types as I won't to be able to give the theme field different values from the db depending on what the SectionID or CourseID are.. – Jemes Mar 19 '10 at 13:06
  • That seems like a much better solution, nice clean view. I'm getting an error now though on my site.master foreach statement cannot operate on variables of type 'object' because 'object' does not contain a public definition for 'GetEnumerator' – Jemes Mar 19 '10 at 13:29
  • My guess is that you haven't quite properly made your Master page strongly typed...check out this post with the instructions: http://stackoverflow.com/questions/768236/how-to-create-a-strongly-typed-master-page-using-a-base-controller-in-asp-net-mvc – Justin Niessner Mar 19 '10 at 13:34
  • Not sure I get what I need to change to make my site.master strongly types. I've updated my Site.Master with the Import code. – Jemes Mar 19 '10 at 14:49