7

We have a bit big blocks of C# code within our cshtml files which must be presented in cshtml and nowhere else (obviously it's not a brilliant case but it's another question).

How we can collapse or hide these blocks of code in order to let our designers work more smoothly? We also want to hide these blocks of code during the demos of the progress with markup.

The real issue is that we also must save the visual representation into SVN.

Is there any native VS 2010 functionality for this or plugin? Maybe there is an opportunity to use "partial" cshtml pages where all the markup will be in one file and all C# code will be in another?

Unfortunately VS isn't going to collapse C# blocks of code within #region directive in such files.

Ultimately there is a similar question Regions In ASP.NET Views? but it gives no answer on how to save the collapsed representation when "Collapse Tag" context menu action item was used.

Community
  • 1
  • 1
xenn_33
  • 855
  • 1
  • 10
  • 25

3 Answers3

15

Try using Visual Studio's collapse functionality. By default I believe the keys are:

[Ctrl+M,Ctrl+H] to hide an arbitrary selection, and
[Ctrl+M,Ctrl+U] to unhide the same ( while collapsed ).

This should allow you to temporarily hide any code. More details available on MSDN

Is this what you were looking for?

Having read a little further you wish to save them collapsed, and apparently .cshtml doesn't support #regions. I guess a hacky solution might be the old:

@if(false){
    <div>
        <!--/*{your long code}*/-->
    </div>
}

Or something to that effect, but you get the idea :)

bPratik
  • 6,894
  • 4
  • 36
  • 67
Joebone
  • 550
  • 4
  • 16
2

Just select your code, right click and select Collapse Tag

dohaivu
  • 2,038
  • 15
  • 17
  • This works for code as it was stated in the original question. But our goal is to save the representation (collapsed or not, or at least the ability to collapse - as with regions) to SVN - obviously we must resolve this thing and the question is about this – xenn_33 Aug 26 '11 at 14:24
0

The way that I see it is that cshtml files are meant for a "user control" side of a presentation layer. If you have too much code in your view files, then I would refactor the code and move re-usable components into partial views. I would then include these partial views through

@Html.RenderPartial("PartialViewName", Model.propertyToRender), or I would use

@{ Html.RenderAction("ActionName", "ControllerName") ;}

  • Unfortunately we already have a sophisticated structure of partial views within given implementation so we cannot leverage such approach. Refactoring is a good idea but we cannot afford it since we are working against very aggressive schedule, so as for current we're searching the way to hide the C# blocks and nothing more – xenn_33 Aug 26 '11 at 08:28
  • Another alternative is to review a process. There are UI controls and there is a presentation logic layer. Designers shoulw worry about UI controls regardless of a presentation logic layer. This is according to Architecting Enterprise Applications book by Dino. Not sure how relevant this is in your scenario. –  Aug 26 '11 at 16:38