10

I've created a new MVC project in Visual Studio 2013 and after creating a view with an empty template (with model) and using a layout page (set to empty), I receive the following:

Razor Error

This causes the view to render incorrectly. I have tried searching for the solution elsewhere to no avail. Cleaning/Rebuilding of the solution does not help either. What's annoying is that it does this on a brand new project.

Any ideas on how to fix?

Additional Info:

Views/Web.config

<?xml version="1.0"?>

<configuration>
  <configSections>
    <sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
      <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
      <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=3.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=5.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.Optimization"/>
        <add namespace="System.Web.Routing" />
        <add namespace="WebShopPortal.Web" />
      </namespaces>
    </pages>
  </system.web.webPages.razor>

  <appSettings>
    <add key="webpages:Enabled" value="false" />
  </appSettings>

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

DownloadViewModel

    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Web.Mvc;

    namespace WebShopPortal.Web.ViewModels
    {
        public class DownloadViewModel
        {
            public string ProductId { get; set; }

            public string DisplayText { get; set; }

            public string DownloadUrl { get; set; }

            public string OptionalReturnText { get; set; }

            [Required(ErrorMessage = "Title is required")]
            public string Title { get; set; }

            [DisplayName("First Name")]
            [Required(ErrorMessage = "First name is required")]
            public string FirstName { get; set; }

            [DisplayName("Last Name")]
            [Required(ErrorMessage = "Last name is required")]
            public string LastName { get; set; }

            [DisplayName("Job Title")]
            public string JobTitle { get; set; }

            [DisplayName("Company Name")]
            [Required(ErrorMessage = "Company name is required")]
            public string CompanyName { get; set; }

            [DisplayName("Company Type")]
            public int CompanyTypeId { get; set; }

            [DisplayName("Address Line 1")]
            public string Address1 { get; set; }

            [DisplayName("Address Line 2")]
            public string Address2 { get; set; }

            [DisplayName("Address Line 3")]
            public string Address3 { get; set; }

            [DisplayName("Address Line 4")]
            public string Address4 { get; set; }

            [DisplayName("City/Town")]
            public string Town { get; set; }

            [DisplayName("State/County")]
            public string County { get; set; }

            [DisplayName("Zip/Postcode")]
            public string Postcode { get; set; }

            public string Country { get; set; }

            [DisplayName("Phone Number")]
            public string WorkTelephone { get; set; }

            [DisplayName("Fax")]
            public string WorkFax { get; set; }

            [DisplayName("Email Address")]
            [EmailAddress(ErrorMessage = "Invalid Email Address")]
            [Required(ErrorMessage = "Email address is required")]
            public string EmailAddress { get; set; }

            public bool DoNotNotify { get; set; }

            public string ReturnUrl { get; set; }

            //Dropdowns
            public IEnumerable<SelectListItem> TitleList { get; set; }
            public IEnumerable<SelectListItem> CompanyTypeList { get; set; }
            public IEnumerable<SelectListItem> CountryList { get; set; } 
        }
    }

_ViewStart.cshtml

@{
    Layout = "~/Views/Shared/_Layout.cshtml";
}

And I can promise you nothing funky is going on in the _Layout page.

Update

Simply closing and reopening the solution seems to make the error disappear.

Pages are rendering fine too. Weird it happens to me after creating a fresh project. I will monitor to see if it happens again in future.

Jack
  • 319
  • 6
  • 16
  • Did you mark your `DownloadViewModel` as inheriting from `WebPageBase`? The ViewModel should only represents the data that the View needs to be rendered. – haim770 Nov 10 '15 at 10:11
  • @haim770 I haven't, it's just a straight-forward ViewModel. namespace WebShopPortal.Web.ViewModels { public class DownloadViewModel { public string ProductId { get; set; } } } – Jack Nov 10 '15 at 10:13
  • Show your `~/Views/Web.Config` and any related `_ViewStart.cshtml` files then. – haim770 Nov 10 '15 at 10:14
  • @Michael DownloadViewModel, I just happened to see if the name was causing an error of which it's not. – Jack Nov 10 '15 at 10:23
  • 1
    Only thing that I can see in your View.config file is that you are missing node and in it node where pageBaseType is defined, for me value for that is System.Web.Mvc.ViewPage. – freshbm Nov 10 '15 at 10:52
  • Just for completeness, could we please see `_Layout.cshtml`, even if you say nothing funky is going on? – Rob Nov 10 '15 at 11:02

1 Answers1

6

It's a long shot but you could try this steps:

  • Run a clean on the solution
  • Unload the project with the issues
  • Delete the .user file that Visual Studio generated next to the
    project
  • Reload the project with the issues
  • Build the solution

Taken from this thread, maybe can help you. It's worth to try.

Community
  • 1
  • 1
freshbm
  • 5,540
  • 5
  • 46
  • 75