passing viewdata to asp.net mvc masterpages
Asked Answered
K

6

6

I'm trying to pass ViewData to my asp.net mvc masterpage for an mvc usercontrol that I keep on a masterpage. For example, I created a dropdownlist of names as an mvc usercontrol and I put that in my masterpage.

The problem I am running into is passing the ViewData to the masterpage. I found this article from Microsoft which has a decent solution but I was wondering if there are other "better" solutions out there. The thing I don't like about the solution in the link is that I have to change every controller to inherit from a new controller class.

http://www.asp.net/learn/MVC/tutorial-13-cs.aspx

Edit: The problem I am looking at is the fact that if I place a user control in my masterpage that relies on ViewData, I have to REPEATEDLY include that ViewData for every single page that uses said masterpage. It's possible the solution in the link above is the best solution but I was hoping there were other alternatives.

Kibitka answered 31/3, 2009 at 7:27 Comment(0)
U
1

For what it's worth, I am using the method from that tutorial in a current project and it works very well.

What you can also do, if it is data that is somewhat static (like a menu that doesn't change much), is to put the object on the cache so your database isn't called for every controller initialisation.

Uncrown answered 31/3, 2009 at 21:3 Comment(0)
S
4

The master page already has access to the ViewData. If you want strongly typed access to it, you need to do two things:

  1. Put the master page stuff in a base class (e.g. CommonViewData)
  2. Have you master page inherit from the generic ViewMasterPage<> class:

    " %>
Sugarcoat answered 31/3, 2009 at 8:4 Comment(1)
The problem I am looking at is the fact that if I place a user control in my masterpage that relies on ViewData, I have to include that ViewData for every single page that uses said masterpage.Kibitka
I
2

Could you possibly use the OnActionExecuting method on a base controller class and populate the view data there?

Something like:

protected override void OnActionExecuting(ActionExecutingContext context)
{
  context.Controller.ViewData.Model = GetDataForControl();
}

I haven't tried it so it's just a thought...

Inflated answered 9/6, 2009 at 19:51 Comment(0)
U
1

For what it's worth, I am using the method from that tutorial in a current project and it works very well.

What you can also do, if it is data that is somewhat static (like a menu that doesn't change much), is to put the object on the cache so your database isn't called for every controller initialisation.

Uncrown answered 31/3, 2009 at 21:3 Comment(0)
B
1

I usually use an abstract controller class for my MasterPage, it is the best solution, because the MasterPage is like an "abstract view". But I override the MasgerPageController View() method to include the viewdata.

protected override ViewResult View(string viewName, string masterName, object model)
{
    this.ViewData["menu"] = this.PagesRepository.GetPublishPages();
    return base.View(viewName, masterName, model);
}
Busty answered 20/10, 2009 at 1:25 Comment(1)
Clean and simple, thanks! Four years later and finally got an upvote ;)Dotation
A
0

I don't quite get your problem...

The problem I am looking at is the fact that if I place a user control in my masterpage that relies on ViewData, I have to REPEATEDLY include that ViewData for every single page that uses said masterpage.

Well yeah... of course you do. If you have a usercontrol in your master page then of course you're going to have to pass the required data for that usercontrol for every action & view that uses that masterpage.

It's not like you have to repeat yourself if you are just inheriting from a base controller.

Is your issue the fact that some controllers have actions that both do and don't call views that derive from that particular masterpage? So therefore if you are implementing a base controller, the actions that don't use that particular masterpage will still have the viewdata for it...? (If all that makes sense ;-)

Aeromarine answered 31/3, 2009 at 21:51 Comment(1)
The whole reason I am asking this question is to see if there are alternatives to the solution provided in the link in my original question. But I guess MS would be posting the "best" solution they can come up with.Kibitka
F
0

I think the solution suggested does work but not the ideal solution. If we put the code in the BaseController constructor it is going to be called even for Action methods which does not have a MasterPages (e.g Post methods and Ajax methods).
I think a better solution(not ideal) is to call Html.Action method in the Master page.

Funny answered 14/2, 2010 at 16:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.