Asp.net-mvc – ASP.NET MVC – Code Behind of Master Pages

asp.netasp.net-mvccode-behind

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

Best Answer

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()); %>