views:

42

answers:

1

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);
    }%>
+3  A: 

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
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
Do you need to use an Anonymous Type?
Justin Niessner
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
I've updated my question.
Jemes
That seems like a much better solution, nice clean view.I'm getting an error now though on my site.masterforeach statement cannot operate on variables of type 'object' because 'object' does not contain a public definition for 'GetEnumerator'
Jemes
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
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