Velter
Velter

Reputation: 2170

Regions In ASP.NET Views?

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?

Upvotes: 76

Views: 56177

Answers (11)

Johnatynaa
Johnatynaa

Reputation: 1

This worked for me :)

<!-- #region
<p> bla bla <p>
@if(blabla)
{

}
#endregion -->

Upvotes: 0

Mustafa &#199;akıroğlu
Mustafa &#199;akıroğlu

Reputation: 1668

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

<!-- #region Test -->

code here

<!-- #endregion -->

Upvotes: 148

fbarikzehy
fbarikzehy

Reputation: 5425

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 -->.

Upvotes: 20

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.

Upvotes: 13

Abdullah Erbey
Abdullah Erbey

Reputation: 420

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

Upvotes: 14

Derek Nigel Bartram
Derek Nigel Bartram

Reputation: 303

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.

Upvotes: 1

Darin Dimitrov
Darin Dimitrov

Reputation: 1039298

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.

Upvotes: 11

eth0
eth0

Reputation: 5137

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

Upvotes: 3

Artur Keyan
Artur Keyan

Reputation: 7683

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

Upvotes: 56

George Stocker
George Stocker

Reputation: 57907

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

Upvotes: 3

mdm20
mdm20

Reputation: 4563

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

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

Upvotes: 3

Related Questions