Error when use "DisplayTemplates\PersonViewModel.cs" and "PersonViewModel.cshtml" (Same name)


I followed exactly the steps described in this post.

But the error is displayed below
ASP.NET runtime error: There is no build provider registered for the extension '.cshtml'. You can register one in the <compilation><buildProviders> section in machine.config or web.config. Make sure is has a BuildProviderAppliesToAttribute attribute which includes the value 'Web' or 'All'.
What am I doing wrong?

file attachments


davidebbo wrote Mar 16, 2013 at 11:17 PM

Not sure that this error is directly related to RazorGenerator. Maybe this thread can help?

Ridermansb wrote Mar 17, 2013 at 2:06 PM

When I remove any reference to RazorGenerator from .csproj the error does not occur.

I tried to copy the Web.config described in the post but the same error appears.
When the edit .csproj with this:

<!--<Import Project="$(SolutionDir)\packages\RazorGenerator.MsBuild.\tools\RazorGenerator.targets"/>
    <Target Name="BeforeBuild">
            <Content Remove="Views\**\*.cshtml" />
            <None Include="Views\**\*.cshtml" />
everything works perfectly.

I do not know what else to do.

davidebbo wrote Mar 18, 2013 at 12:44 AM

Can you try my test project? Knowing whether that works would help determine whether the issue is with your project or something with the way the machine/VS is set up.

Ridermansb wrote Mar 18, 2013 at 2:42 PM

I think I found the problem!
Create: http://cl.ly/NfOH
  1. Add a controller PersonController
  2. Create class ViewModel
public class PersonViewModel
    public string Name { get; set; }
  1. Create view Index.cshtml
@model PersonViewModel
  1. Create in DisplayTemplates folder the PersonViewModel.cshtml
@model PersonViewModel
@Html.EditorFor(p => p.Name)
  1. Try compile

jdiekhoff wrote May 3, 2013 at 3:31 PM

I had this same issue when trying to use DisplayTemplates for DateTime properties and here's how I fixed it:

Recreate Issue:

Create a DateTime.cshtml file in the DisplayTemplates folder:
@model DateTime
Try to compile and you'll receive errors:
'(AssemblyName).Views.Shared.DisplayTemplates.DateTime' does not contain a definition for 'Date' and no extension method 'Date' accepting a first argument of type '(AssemblyName).Views.Shared.DisplayTemplates.DateTime' could be found.

Why this is happening: Because the view page is getting compiled and has the same name as the model, the compiler tries to use the compiled view page as the model for itself. If you look carefully at the error you'll see that it's trying to access the .Date property of the compiled DateTime view, which will throw an error because it doesn't exist.

How to fix it so that the right model is used: Explicity type out the namespace for the model that you want to use:
@model System.DateTime <- Instead of just DateTime, give the full namespace
This will now compile as expected.