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?

1
  • You can use Ctrl+M, Ctrl+L to collapse the tags May 2, 2012 at 9:50

10 Answers 10

143

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

<!-- #region Test -->

code here

<!-- #endregion -->
10
  • If you have VS 2012, then download Web Essentials 2012. In either case, you'll need to go to your menu in VS (2012 for me), Tools > Extensions and Updates > Online and search for the tool. In short, IMO, this is definitely the answer until MS provides built-in support.
    – dotnetN00b
    Jul 10, 2014 at 14:52
  • 11
    The syntax is as simple as <!-- #region Test --> and <!-- #endregion -->.
    – dotnetN00b
    Jul 10, 2014 at 15:04
  • 1
    Works with VS2015 with no plugin - nice & perfect answer
    – Catto
    Nov 3, 2015 at 10:39
  • 1
    This is the only correct answer here, the others are talking about collapsing the PARENT TAG, which is not the same thing Feb 11, 2016 at 15:29
  • 1
    This should be the correct answer! Works with VS2015 without any plugin.
    – Luke Vo
    Feb 23, 2016 at 17:33
56
+50

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

7
  • 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 1, 2012 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 3, 2012 at 11:55
  • 1
    suggest the answer starts: No, you can't use regions, but you could try this instead
    – Myster
    Feb 20, 2013 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. Apr 25, 2013 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 (stackoverflow.com/questions/6457967) May 8, 2014 at 15:57
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 -->.
3
  • 1
    Definitely the better option in recent VS versions - and more natural
    – Jon Story
    Jul 1, 2016 at 16:38
  • Works for me in VS2013 Jun 12, 2018 at 13:19
  • Works for me in Visual Studio 2019 Professional
    – wesreitz
    Jul 28, 2021 at 7:38
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

1
  • Points for the Outlining Collapse. I find it very practical.
    – ProfK
    Dec 12, 2014 at 20:00
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.

1
  • 2
    This is perfect (y) I love it. Thx so much
    – Mr. Robot
    Feb 25, 2016 at 23:49
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.

1
  • 1
    Useless, as they do not save when closed.
    – Ed DeGagne
    Feb 21, 2014 at 16:39
3

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

<div id="BLABLA">...</div>
3

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

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! :)

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.

1
  • Or, just be cautious about where you are placing the regions so they don't break the c# code embedded in HTML Mar 2 at 19:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.