Visual Studio 2012 code formatting on CSHTML lowercases generic model types
Asked Answered
T

3

8

When formatting code a Razor cshtml file in Visual Studio 2012 (with Ctrl + K + D) if model is a generic type, VS makes it all lowercase. For example:

@model IEnumerable<Content>

converts to (consider all lower case content):

@model IEnumerable<content>

after formatting. This cause the code not to compile. Is this a bug or I'm missing something? How it can be fixed?

Tacye answered 19/9, 2012 at 8:43 Comment(2)
Bug submitted aspnetwebstack.codeplex.com/workitem/828. Just upvote it.Scribbler
This still happens with VS 2013: While the setting "HTML -> Formatting -> As entered" will preserve the casing the source will still get indented unneccesarilly.Inculpate
F
11

This issue is from Visual Studio 2012 not recognizing it as razor code and treating it as regular HTML. I believe this is a bug with VS 2012 and will hopefully get fixed soon. As a temporary fix, you can do this:

  1. Goto TOOLS -> OPTIONS
  2. Select Text Editor -> HTML -> Formatting
  3. Set the Client Tag drop down to "As entered"

Hope this helps.

Forster answered 2/10, 2012 at 16:1 Comment(0)
F
2

If this is a new project in VS 2012, you need to add this line of code in your <appSettings>section of your main web.configfile:

<add key="webpages:Version" value="1.0.0.0"/>

If this was a pre-existing project you probably already have that key in your web.config. Make sure it is for version 1.0.0.0 and not 2.X or 1.2.X

Also ensure that your project reference to System.Web.WebPages is for version 1.0.0.0 and not 2.0.0.0

After you make these changes you have to then close and re-open Visual Studio 2012!

Forster answered 2/10, 2012 at 16:37 Comment(3)
The last answer I gave was more of a crutch for the real problem.Forster
What if you are using WebPages 2.0.0.0 ? MVC4 ?Entebbe
My other solution below should do the trick. You have to set the formatting of Client tags to as entered. Just remember to keep you parameters lowercase to conform with CSS standards since Ctrl + K + D will no longer automatically make them lowercase.Forster
P
0

The following worked for me with Visual Studio Professional 2012, ASP.NET MVC 5 and Microsoft.System.Web.WebPages 3.0:

<add key="webpages:Version" value="3.0.0.0" />

But this solution did not work immediately

At first, Chris Haddox's solution regarding webpages:Version:

<add key="webpages:Version" value="1.0.0.0"/>

did not work for me, I already had this key and it was set to 1.0.0.0.

But since I am using Visual Studio Professional 2012, ASP.NET MVC 5 and Microsoft.System.Web.WebPages 3.0...

This issue showed me that my Web.Config webpages:Version was set to 1.0.0.0, I changed it to 3.0.0.0 because I read in another article that might be correct for Microsoft.System.Web.WebPages 3.0 and I still had the formatting bug for a little while.

I continued to make edits to my .cshtml file. Then, all of the sudden formatting started working correctly. Maybe from building, but I do not think that I did do a build, but can't be sure, and I know that I did not restart Visual Studio 2012.

Paulapauldron answered 5/12, 2015 at 0:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.