Master does not have a definition for ViewData (ASP.NET MVC2)

04 Aug 2010

I'm building an ASP.NET MVC2 web application and I was trying to access ViewData in my Site.Master page and I kept getting "master does not have a definition for ViewData".

[code lang="xml"]<%= ViewData["topic"] %>[/code]

The simple answer was that my Master Page was not an ASP.NET MVC2 master page. Somehow I had a standard master page.

So, all I did was swap the existing page declaration

[code lang="xml"]<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site.Master.cs" Inherits="HubSubscriber.Views.Shared.Site" %>[/code]

With an ASP.NET MVC2 declaration:

[code lang="xml"]<%@ Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage" %>[/code]

And bingo! ViewData was now accessible from my master page.

This post on Passing Data to View Master Pages may also be useful.