我正在使用 razor 引擎制作 ASP.NET MVC 应用程序。
我想知道是否可以在视图中使用 区域 。
像这样:
#region blabla
<p>@Model.Name</p>
<p>...</p>
#endregion
这不起作用。还有其他选择吗?
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?
发布评论
评论(11)
这适用于 Visual Studio 2015 及更高版本
(感谢
@dotnetN00b
在评论部分提供示例):This works in Visual Studio 2015 and above
(thanks to
@dotnetN00b
for the sample in the comments section):选择需要转换为区域的部分,然后右键单击并按 CollapseTag
Select the part which needs to be converted to region, then right click and press CollapseTag
在 Visual Studio(2015 及更高版本).html 或 .cshtml 代码编辑器中,键入
region
,然后按Tab
键。这实现了#region
代码段代码如下:In Visual Studio (2015 and above) .html or .cshtml code editor, type
region
, then pressTab
key.This implements#region
snippet code like below:在 Visual Studio 中,您可以手动添加轮廓区域,如下所示:
来自 MSDN
但这并不实际。
对于 HTML,您可以在文本编辑器选项中手动编辑每个标签的大纲选项:
最小行数的最小值为 1 才有效。
有关 MSDN 的详细信息
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
我的上下文菜单中没有“CollapseTag”选项。我通常做的是:
或
使用 Ctrl+M、Ctrl+H
我使用的是 Microsoft Visual Studio Pro 2013。
I don't have "CollapseTag" option in my context menu. What I usually do is :
or
use Ctrl+M, Ctrl+H
I am using Microsoft Visual Studio Pro 2013.
不,据我所知,不可能在视图中使用区域。您可以使用局部视图将视图区域分组为可重用的局部视图。
请参阅新答案;它有效并达到了预期的效果。
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.
Div 是可折叠的,因此您始终可以将它们与某种 id 一起使用来模拟区域。
Divs are collapsible so you could always use them with some sort an id to kind of mimic regions.
您可以将母版页与
RenderPartial
或RenderAction
结合使用来缩小视图。两者都有自己的位置。You can use Masterpages with
RenderPartial
orRenderAction
to make your views smaller. Both have their places.区域对我来说在视图中起作用,我可以定义一个区域,但它不会崩溃。如果您使用 @Artur 的使用 Collapse Tag 的方法,那么您几乎就可以了! :)
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! :)
请注意,使用区域可能会导致视图出现问题 - 尽管它们在语法上是有效的,但代码和 HTML/SCRIPT 之间的指定通常会变得“混乱”,从而导致不可预测的行为。
DIV 无疑是“更好”的解决方案,特别是额外的 DIV 可以在稍后更改 CSS 样式时提供更大的灵活性。
如果您需要大量区域,请考虑进一步重构您的代码。
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.
这对我有用:)
This worked for me :)