Wednesday, 15 September 2010

Error CS1061 upgrading to ASP.NET MVC3 Preview 1

I just encountered a problem upgrading a client's website from ASP.NET MVC2 to the preview of MVC3:

Compiler Error Message: CS1061: 'object' does not contain a definition for 'StartDate' and no extension method 'StartDate' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)

This error happens at runtime when I go to view a particular View in the browser. The view's Model property was not being typed as my custom type and instead was reverting to the default type of System.Object.

Turns out it's a known bug that is caused by the fact that the particular view's .aspx page also has a <% Import %> directive after the <% Page %> directive.

The workaround (until the next preview is released) is to swap the two lines around, so that the <% Page %> is the last directive in the file. eg.

<%@ Import Namespace="Client.Models" %>
<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<Client.Models.CustomViewModel>" %>
 

1 comment:

Higor Ramos said...

Thank you! Simple and very useful explanation.