views:

25

answers:

2

I am using Visual Studio 2010, but this applies to VS 2008 as well.

In my MVC views I prefer to write my coded elements as follows:

<% if (somecondition) { %>
    <p>something</p>
<% } else { %>
    <p>something else</p>
<% } %>

This just looks more readable to me than the following which is what VS auto corrects my code to look like:

<% if (somecondition) 
   {  %>
    <p>something</p>
<%}
  else
  {  %>
    <p>something else</p>
  <%} %>

I usually stop my work to format the code back into the fashion that I prefer, but am getting tired of this. I love the auto-correcting in VS, but want this to cease only in my views, and only for what I describe above. Is this possible?

A: 

Yeah, it's annoying. Here's how to turn it off:

  1. Go to Tools -> Options
  2. In the Text Editor option look for t`HTML1 and expand the node.
  3. In the Formatting option, turn off all Automatic formatting options.

Alternatively, in the same dialog, you can configure the formatting for each specific tag. Just click on the Tag Specific Options... button.

Esteban Araya
A: 

Right click within your view then:

Pick Formatting and Validation

Expand: C# -> Formatting -> Newlines

Then uncheck 'Place open brace on new line for control blocks'

Nicholas Murray
that works for the view, but now I have the opposite problem in my .cs files in that it now auto-corrects my code blocks to put the opening brace on the same line.... if (somecondition) { ....I can live that I guess, as it's more important to me to have the mixed-mode view look clean.
mikerennick
@mikerennick - Yin and Yang!
Nicholas Murray