asp.net-mvc ASP.NET 视图中的区域?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5392931/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Regions In ASP.NET Views?
提问by Velter
I am making an ASP.NET MVC application with the razor engine.
And I was wondering if it's possible to use Regionsin a view.
我正在使用剃刀引擎制作 ASP.NET MVC 应用程序。
我想知道是否可以在视图中使用区域。
something like:
就像是:
#region blabla
<p>@Model.Name</p>
<p>...</p>
#endregion
This does not work. Is there an alternative?
这不起作用。有替代方案吗?
回答by Mustafa ?ak?ro?lu
If you download Web essential 2013 you can use regions both in your cshtml and javascript files.
如果您下载 Web Essential 2013,您可以在 cshtml 和 javascript 文件中使用区域。
Like this (thanks to @dotnetN00b
for the sample in the comments section):
像这样(感谢@dotnetN00b
评论部分的示例):
<!-- #region Test -->
code here
<!-- #endregion -->
回答by Artur Keyan
Select the part which needs to be converted to region, then right click and press CollapseTag
选择需要转换为区域的部分,然后右键单击并按 CollapseTag
回答by Abdullah Erbey
In Visual Studio, you can manually add outlined region like this :
在 Visual Studio 中,您可以像这样手动添加轮廓区域:
To create or remove a collapsible region
Select the text you want to treat as a collapsible region.
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.
To remove a collapsible region, collapse it, and then click it to select it.
On the Edit menu, point to Outlining, and then click Stop Hiding Current.
To collapse and expand a single region
To collapse a region, click the minus sign (-) in the margin of the editor.
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.
创建或删除可折叠区域
选择要视为可折叠区域的文本。
若要创建可折叠区域,请在“编辑”菜单上指向“大纲”,然后单击“隐藏选择”。
编辑器将所选内容转换为一个区域,将其折叠,并显示一个带有省略号 (...) 的框,以指示该区域包含折叠区域。您可以将鼠标指针悬停在框上以查看其内容。
要移除可折叠区域,请将其折叠,然后单击以将其选中。
在编辑菜单上,指向大纲,然后单击停止隐藏当前。
折叠和展开单个区域
要折叠区域,请单击编辑器边距中的减号 (-)。
要展开折叠区域,请单击边距中的加号 (+)。
折叠和展开所有区域
在编辑菜单上,指向大纲,然后单击切换所有大纲。
But that's not really practical.
但这并不实际。
For HTML you can manually edit the outline option for each tags in the text editors options :
对于 HTML,您可以在文本编辑器选项中手动编辑每个标签的大纲选项:
Minimum value of minimum lines is 1 to be effective.
最小行数的最小值为 1 才有效。
回答by Fereydoon Barikzehy
In Vs2015 and Resharper type "region" in your html or cshtml page, then press Tab key.This insert #region snippet code.
在 Vs2015 和 Resharper 中,在您的 html 或 cshtml 页面中输入“region”,然后按 Tab 键。这会插入 #region 片段代码。
<!-- #region name -->
//Your html or cshtml codes
<!-- #endregion -->
回答by Protect Life Save Forests
I don't have "CollapseTag" option in my context menu. What I usually do is :
我的上下文菜单中没有“CollapseTag”选项。我通常做的是:
- Select text.
- Goto Edit -> Outlining -> Hide Selection.
- 选择文本。
- 转到编辑 -> 大纲 -> 隐藏选择。
or
或者
use Ctrl+M, Ctrl+H
使用 Ctrl+M、Ctrl+H
I am using Microsoft Visual Studio Pro 2013.
我正在使用 Microsoft Visual Studio Pro 2013。
回答by Darin Dimitrov
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.
不,AFAIK 不可能在视图中使用区域。您可以使用局部视图将视图区域分组为可重用的局部视图。
See the newer answer; it works and accomplishes the desired effect.
请参阅较新的答案;它可以工作并达到预期的效果。
回答by mdm20
Divs are collapsible so you could always use them with some sort an id to kind of mimic regions.
Div 是可折叠的,因此您始终可以将它们与某种 id 一起使用以模拟区域。
<div id="BLABLA">...</div>
回答by George Stocker
You can use Masterpages with RenderPartial
or RenderAction
to make your views smaller. Both have their places.
您可以将 Masterpages 与RenderPartial
或RenderAction
一起使用来缩小您的视图。两者都有自己的位置。
回答by eth0
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! :)
区域对我来说在视图中工作,我可以定义一个区域但它不会崩溃。如果您使用@Artur 使用折叠标签的方法,您几乎就在那里!:)
回答by Derek Nigel Bartram
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.
请注意,使用区域可能会导致视图出现问题 - 即使它们在语法上有效,但代码和 HTML/SCRIPT 之间的名称通常会变得“混乱”,从而导致不可预测的行为。
DIVs are certainly the 'better' solution, especially as extra DIVs allow more flexibility when changing CSS styles later.
DIV 无疑是“更好”的解决方案,特别是因为额外的 DIV 可以在以后更改 CSS 样式时提供更大的灵活性。
If you need lots of regions, then consider refactoring your code further.
如果您需要大量区域,请考虑进一步重构您的代码。