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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 00:35:10  来源:igfitidea点击:

View() vs. PartialView()

asp.net-mvc

提问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 _Layoutpage:

我认为最大的区别在于_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.aspxand Index.ascxfor the same controller having both methods might help disambiguate. But I agree that in general Viewis more commonly used. And PartialViewcan also be used to load full views, it's just that it's semantically wrong.

如果您拥有Index.aspx并且Index.ascx对于具有这两种方法的同一个控制器可能有助于消除歧义。但我同意一般View情况下更常用。而且PartialView还可以用来加载全景,这只是它的语义错误。