views:

18

answers:

2

In the "Admin" area of my application, an object must be available in ViewData on every page (for display in the Master template). I have already inherited from Controller, so I cannot make a simple base class that handles it. What is a good solution of doing this when not using inheritance? An ActionFilter seems interesting but I don't want to put it on every controller in the Admin area. I'm considering the following:

  • Custom ControllerFactory that detects Area as well
  • Application_BeginRequest(), though I have no knowledge on executing controller then.

Maybe you have a better solution?

A: 

i have a dropdown on my masterpage. you dont need viewdata for it. i did it like this

code on masterpage:

<%= Html.DropDownList("schselectr", MVC2_NASTEST.MvcApplication.masterSchooljaarList())%>

in Global.asax.cs

    public static SelectList masterSchooljaarList() {
        NASDataContext _db = new NASDataContext();
        List<Schooljaar> newlist = _db.Schooljaars.ToList();
        return new SelectList(_db.Schooljaars.ToList(), "Sch_Schooljaar", "Sch_Schooljaar");
    }

so simply, it calls the method, which returns the data i need, every time you load the page. easy, clean, effective.

Stefanvds
I just commented that the data object is the currently logged in user, so has dependencies on both database and IPrincipal. Making a static context in a test-friendly framework like this isn't a good solution anyway. But thanks for answering of course!
ciscoheat
cant you send the IPrincipal with it? <%= Html.DropDownList("schselectr", MVC2_NASTEST.MvcApplication.masterSchooljaarList(HttpContext.Current.User.Identity))%>
Stefanvds
@Stefanvds, The reason you don't want to do it this way is because it violates a lot of design patterns (including mvc) and it is hard to test.
Mattias Jakobsson
A: 

In this case I would create a separate action that executes a partial view that shows the data you need. In my opinion this is the most clean solution for this kind of problem and it's easily testable and reusable.

Mattias Jakobsson