开发者

ASP.NET MVC 2 - Set ViewData on every request in specific area (without inheritance)

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 ar开发者_StackOverflow社区ea. 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?


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.


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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜