Blog

Home/ASP.NET MVC/The name ‘model’ does not exist in the current context

The name ‘model’ does not exist in the current context

This blog post is about the error “CS0103: The name ‘model’ does not exist in the current context’ when developing with EPiServer 7 Preview.

Background & cause

IntelliSense won’t recognize the keyword @model in your view.

model-does-not-exist

When running the site we get the yellow screen of death.

yellow-screen-of-death-model-does-not-exist

This happened because I manually created the “Views”-folder and forgot to add the necessary configuration files that otherwise is auto-generated by Visual Studio for MVC 4 websites.

Resolution

Create a web.config directly beneath the “Views”-folder and add the configuration below (this is the default configuration added by Visual Studio when creating an ASP.NET MVC 4 website).

<?xml version="1.0"?>

<configuration>
  <configSections>
    <sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
      <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
      <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
    </sectionGroup>
  </configSections>
  <system.web.webPages.razor>
    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <pages pageBaseType="System.Web.Mvc.WebViewPage">
      <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.webPages.razor>

  <system.web>
    <pages
        validateRequest="false"
        pageParserFilterType="System.Web.Mvc.ViewTypeParserFilter, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
        pageBaseType="System.Web.Mvc.ViewPage, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
        userControlBaseType="System.Web.Mvc.ViewUserControl, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
      <controls>
        <add assembly="System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" namespace="System.Web.Mvc" tagPrefix="mvc" />
      </controls>
    </pages>
  </system.web>

  <system.webServer>
    <validation validateIntegratedModeConfiguration="false" />

    <handlers>
      <remove name="BlockViewHandler"/>
      <add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
    </handlers>
  </system.webServer>
</configuration>

Make sure to close and re-open your index file after having added the configuration file. Now you should have access to the @model keyword and can access the model through @Model.

model-exists

Hope this helps anyone that might – like me – be a little careless when deleting auto-generated project resources.

Written by

Architect, developer

  • http://twitter.com/Sir_MinhTien Tiến Bùi

    Nice, I’m looking for this one. Thanks!

  • Jose

    I have spent hours fighting this bug in a remote server. Thanks for taking the time to write it down!!!

    • http://twitter.com/bergdaniel Daniel Berg

      Awesome. Glad it helped! :-)

  • Aleks

    That did the trick. Thanks for posting this, as it saved me a ton of time.

  • http://www.edje.se/ Rikard Edje

    Thank you sir!

  • sDhiraj

    thanks

  • TJ

    I needed that. I was comparing to another existing project I was still missing parts of the config file. THANK YOU!

  • Sam Martin

    Thanks!

  • ajay

    i want to export table in to excel using mvc2
    in view page how to fill model in to export to excel button