asp.net-mvc View() 与 PartialView()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4210138/
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
View() vs. PartialView()
提问by burnt1ce
The View()
method can load Partial Views.
该View()
方法可以加载部分视图。
Is the difference between View()
and PartialView()
is that View()
can load views and partial views and PartialView()
can only load partial views?
之间的差别View()
,并PartialView()
是View()
可以加载视图和局部视图,并PartialView()
只能装载部分的看法?
回答by Eilon
It's up to a view engine to decide if they want to treat partial views different from regular views.
由视图引擎决定是否要处理与常规视图不同的部分视图。
For example, in the WebFormViewEngine there is no difference.
例如,在 WebFormViewEngine 中没有区别。
In the new ASP.NET MVC 3 RazorViewEngine there are some differences. Only regular views will have the "_viewstart.cshtml" pages run because they are meant for things such as setting up layout pages.
在新的 ASP.NET MVC 3 RazorViewEngine 中存在一些差异。只有常规视图才会运行“_viewstart.cshtml”页面,因为它们用于设置布局页面等。
回答by fabriciorissetto
I think the biggest difference is about the use of the _Layout
page:
我认为最大的区别在于_Layout
页面的使用:
- PartialView(): the razor engine will get the view (e.g. index.cshtml) without any layout page (_layout.cshtml).
- View(): the engine will get your view (e.g. index.cshtml) and then appends the content of this view inside the layout page (_layout.cshtml)
- PartialView():razor 引擎将获得没有任何布局页面(_layout.cshtml)的视图(例如 index.cshtml)。
- View():引擎将获取您的视图(例如 index.cshtml),然后将该视图的内容附加到布局页面(_layout.cshtml)中
回答by Darin Dimitrov
If you have Index.aspx
and Index.ascx
for the same controller having both methods might help disambiguate. But I agree that in general View
is more commonly used. And PartialView
can also be used to load full views, it's just that it's semantically wrong.
如果您拥有Index.aspx
并且Index.ascx
对于具有这两种方法的同一个控制器可能有助于消除歧义。但我同意一般View
情况下更常用。而且PartialView
还可以用来加载全景,这只是它的语义错误。