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?
In Vs2015 and Resharper type "region" in your html or cshtml page, then press Tab key.This insert #region snippet code.
You can use Masterpages with
RenderPartial
orRenderAction
to make your views smaller. Both have their places.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.
In Visual Studio, you can manually add outlined region like this :
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 :
Minimum value of minimum lines is 1 to be effective.
More info on MSDN
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.
Select the part which needs to be converted to region, then right click and press CollapseTag