74

I am making an ASP.NET MVC application with the razor engine.
And I was wondering if it's possible to use Regions in a view.

something like:

#region blabla
    <p>@Model.Name</p>
    <p>...</p>
#endregion

This does not work. Is there an alternative?

Chuck Norris
  • 15,207
  • 15
  • 92
  • 123
Velter
  • 2,080
  • 1
  • 16
  • 19

10 Answers10

143

This works in Visual Studio 2015 and above (thanks to @dotnetN00b for the sample in the comments section):

<!-- #region Test -->

code here

<!-- #endregion -->
Sameer Alibhai
  • 3,092
  • 4
  • 36
  • 36
Mustafa Çakıroğlu
  • 1,618
  • 1
  • 11
  • 10
56

Select the part which needs to be converted to region, then right click and press CollapseTag

Artur Keyan
  • 7,643
  • 12
  • 52
  • 65
  • 2
    I never knew that you could do this although I can't see how to give the region a name. That said, I can't see any reason why this should not be marked as the correct answer? – Dangerous May 01 '12 at 13:27
  • 5
    In fact, this is the correct answer. This answer has been awarded bounty by Chuck Norris, so No matters where the green tick is. This is the correct one. – Jonathan May 03 '12 at 11:55
  • 1
    suggest the answer starts: No, you can't use regions, but you could try this instead – Myster Feb 20 '13 at 23:27
  • 17
    Note that this is not saved when your close and reopen the file in question, which makes them not really interesting at least in my case. – Joel Peltonen Apr 25 '13 at 12:20
  • 4
    Also note this won't actually allow you to collapse a group of elements, but their parent and/or individual elements, which won't give the desired effect in this case. In my case, I'm looking for collapsable `#region` elements within a code block. Since I'm using asp.net in MVC, I put the region demark and then use close/end server tags. It's ugly when expanded but it works: `<% // non-collapsed code .. %><% #region collapsable code #endregion %><% // resume non-collapsed code.. %>` OR you can use Ctrl-M + Ctrl-H & Ctrl-M + Ctrl-U (http://stackoverflow.com/questions/6457967) – JoeBrockhaus May 08 '14 at 15:57
  • 1
    These are not regions at all, just collapsing tags. Even Chuck Norris can be wrong sometimes. – Arturo Torres Sánchez Dec 10 '14 at 23:50
  • I dont have the CollapseTag option when i rightclick in VS2017, someone know how can turn it on? – Juan Salvador Portugal Sep 10 '18 at 15:05
20

In Visual Studio (2015 and above) .html or .cshtml code editor, type region, then press Tab key.This implements #region snippet code like below:

<!-- #region name -->

//Your html or cshtml codes

<!-- #endregion -->.
fbarikzehy
  • 4,885
  • 2
  • 33
  • 39
14

In Visual Studio, you can manually add outlined region like this :

To create or remove a collapsible region

  1. Select the text you want to treat as a collapsible region.

  2. To create a collapsible region, on the Edit menu, point to Outlining, and then click Hide Selection.

The editor turns the selection into a region, collapses it, and displays a box with an ellipsis (...) to indicate that the area contains a collapsed area. You can hold the mouse pointer over the box to see its contents.

  1. To remove a collapsible region, collapse it, and then click it to select it.

  2. On the Edit menu, point to Outlining, and then click Stop Hiding Current.

To collapse and expand a single region

  1. To collapse a region, click the minus sign (-) in the margin of the editor.

  2. To expand a collapsed region, click the plus sign (+) in the margin.

To collapse and expand all regions

On the Edit menu, point to Outlining, and then click Toggle All Outlining.

From MSDN

But that's not really practical.

For HTML you can manually edit the outline option for each tags in the text editors options :

enter image description here

enter image description here

Minimum value of minimum lines is 1 to be effective.

More info on MSDN

Abdullah Erbey
  • 420
  • 5
  • 13
13

I don't have "CollapseTag" option in my context menu. What I usually do is :

  1. Select text.
  2. Goto Edit -> Outlining -> Hide Selection.

or

use Ctrl+M, Ctrl+H

I am using Microsoft Visual Studio Pro 2013.

11

No, AFAIK it is not possible to use regions in a view. You could use partials to group regions of the view into reusable partial views.

See the newer answer; it works and accomplishes the desired effect.

Community
  • 1
  • 1
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
3

regions sort-of work in views for me, I can define a region but it will not collapse. If you use @Artur's method of using Collapse Tag you're pretty much there! :)

Chuck Norris
  • 15,207
  • 15
  • 92
  • 123
eth0
  • 4,977
  • 3
  • 34
  • 48
3

Divs are collapsible so you could always use them with some sort an id to kind of mimic regions.

<div id="BLABLA">...</div>
mdm20
  • 4,475
  • 2
  • 22
  • 24
3

You can use Masterpages with RenderPartial or RenderAction to make your views smaller. Both have their places.

George Stocker
  • 57,289
  • 29
  • 176
  • 237
1

Be aware that using regions can cause issues in views - even though they are syntactically valid, often the designation between code and HTML/SCRIPT becomes 'confused', resulting in unpredictable behavior.

DIVs are certainly the 'better' solution, especially as extra DIVs allow more flexibility when changing CSS styles later.

If you need lots of regions, then consider refactoring your code further.