ASP.NET MVC - Code Behind of Master Pages
Asked Answered
G

5

6

I am newbie for ASP.NET MVC 1.0. I am converting from a classic application built up with VS2008 .NET3.5. I created a master page, and the menu must be read from the database. Now the code that generate the HTML into the appropriate menu div in classic ASP.NET3.5 VS2008 was in the code behind of the master page.

I cannot understand now where the code beind of the master page is in ASP.NET MVC 1.0?

Anyone has examples?

Thanks

Greasewood answered 9/11, 2009 at 8:54 Comment(0)
O
7

In MVC there are no longer Code-Behind classes. What you want is a Partial.

You'd use it like so:

<% Html.RenderPartial("MainMenu.ascx", ViewData["Menu"]); %>

If this Menu is going to be in all of your pages you can make your controllers subclass a custom controller class that always fills the Menu data first.

If messing with the MVC inheritance hierarchy is overkill you can also make a MenuController class and use the RenderAction in your view/master:

<% Html.RenderAction<MenuController>(x => x.MainMenu()); %>
Oxy answered 9/11, 2009 at 9:3 Comment(0)
Z
6

You can still have code behind if you want. In your .master file put:

<%@ Master Language="C#" AutoEventWireup="true" 
Inherits="Site_Master" CodeFile="Site.Master.cs" %>

Then in your .master.cs:

public partial class Site_Master : ViewMasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
}
Zhukov answered 9/11, 2009 at 14:47 Comment(3)
Well, he asked - I answered. Besides, there could be a legal case, you never know.Zhukov
ditto - he asked, this is the answer.Selfeffacement
I migrated a ten year old asp.net web site to new project types and needed to do this. Hopefully we can remove it later.Economize
S
3

Your master page is now a View, and Views should be passive. In other words, they shouldn't go look up things themselves.

It would be a much more correct approach (within the context of ASP.NET MVC) to pull the required data from the Model.

Take a look at this SO question for a related discussion.

Sech answered 9/11, 2009 at 9:0 Comment(2)
I think the referenced example, with the ViewModel objects is a little bit overkill for the question.Megillah
@cottsak I don't; because once you start using ViewModels, you realize that you're glad you don't do it the magic string way.Contrabass
B
3

There is a great tutorial on the ASP.NET site that shows how to do exactly this.

Briefly, you pass the data to the master page through the ViewData collection. To get the data into ViewData, create an application level controller. Have the page controllers inherit from the application controller instead of the base MVC controller.

Also, if you need to do things on your master page in reaction to the page being displayed, through this application controller you can tie into the ActionExecuting event. That will provide you information about the context of the page request currently being process.

Buckskin answered 9/11, 2009 at 14:42 Comment(1)
That is an awesome tutorial, explains the Controller extending option pretty well.Oxy
S
1

Personally, I prefer using strongly typed views and ViewModels. If your master page requires data, then create a ViewModel for it. Ensure that every page's ViewModel inherits from this base ViewModel. Similarly, create a base controller that every other controller inherits from. Using Action Filters will allow you to ensure that the master ViewModel is populated implicitly. See this for an example.

Substation answered 9/11, 2009 at 14:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.