Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mvc 5 ViewPage<T> becomes non generic ViewPage when compiled

I'm upgrading a site by creating a new ASP.Net Mvc 5 skeleton and then dropping in the content. Currently all seems to be working except for the strongly typed views. All Model property access fails and it's evident that the nongeneric controller is used.

Example:

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

<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
    <h1><%= Model.Title %></h1>
</asp:Content>

I get the error 'object' does not contain a definition for 'Title' and no extension method...

Going to Definition on Model takes me here:

namespace System.Web.Mvc
{
    // Summary:
    //     Represents the properties and methods that are needed to render a view as
    //     a Web Forms page.
    [FileLevelControlBuilder(typeof(ViewPageControlBuilder))]
    public class ViewPage : Page, IViewDataContainer
    {
        <snip>
        public object Model { get; }

How come it chooses the wrong item?

The project was created as a MVC4 project and then upgraded to MVC5 using nuget before any code was added.

like image 300
Carl R Avatar asked Oct 26 '13 19:10

Carl R


2 Answers

Sadly, it doesn't appear that the Nuget packages for ASP.NET MVC 5 does not handle the needed web.config and code changes to support everything in ASP.NET MVC 5. Take a look at http://www.asp.net/mvc/tutorials/mvc-5/how-to-upgrade-an-aspnet-mvc-4-and-web-api-project-to-aspnet-mvc-5-and-web-api-2 which should help you do the needed manual upgrade steps that the Nuget package doesn't cover.

like image 145
Steven V Avatar answered Nov 10 '22 06:11

Steven V


For me I was missing this specficially in my View/web.config since the default config assumes razor.

  <system.web>
    <compilation>
      <assemblies>
        <add assembly="System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
      </assemblies>
    </compilation>

    <pages pageParserFilterType="System.Web.Mvc.ViewTypeParserFilter, System.Web.Mvc, 
       Version=5.2.3.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
           pageBaseType="System.Web.Mvc.ViewPage, System.Web.Mvc, 
       Version=5.2.3.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
       userControlBaseType="System.Web.Mvc.ViewUserControl, System.Web.Mvc, 
       Version=5.2.3.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
           >
      <namespaces>
        <add namespace="System.Web.Mvc"/>
        <add namespace="System.Web.Mvc.Ajax"/>
        <add namespace="System.Web.Mvc.Html"/>
        <add namespace="System.Web.Routing"/>
      </namespaces>
    </pages>
  </system.web>
like image 38
jbtule Avatar answered Nov 10 '22 07:11

jbtule