在ASP中,视图和部分视图的区别是什么?净MVC吗?

时间:2022-11-30 17:28:51

What is the difference between a View and a PartialView in ASP.NET MVC?

在ASP中,视图和部分视图的区别是什么?净MVC吗?

At first glance the need for both seems non-obvious to me.

乍一看,对我来说两者的需求似乎并不明显。

6 个解决方案

#1


38  

In theory, the answer is: A partial view is a "sub-view" that you embed within a main view - something that you might reuse across multiple views, like a sidebar.

理论上,答案是:部分视图是一个“子视图”,您将它嵌入到主视图中——您可以跨多个视图重用它,比如侧边栏。

In practice, the answer is: Very little.

实际上,答案是:非常少。

In theory, partial views are more lightweight than standard views, but it's perfectly OK to pass a "regular" view to RenderPartial and the performance seems to be exactly the same. I frequently use regular .aspx views as "partial" views because you can make them reference a master view in order to provide templated content like what you can do with UserControls in ASP.NET WebForms. See here.

理论上,部分视图比标准视图更轻量级,但是将“常规”视图传递给RenderPartial是完全可以的,而且性能似乎完全相同。我经常使用常规的.aspx视图作为“部分”视图,因为您可以使它们引用一个主视图,以便提供模板化的内容,就像您可以在ASP中使用UserControls一样。净WebForms。在这里看到的。

Partial views are more like web parts on a portal - they are completely self-contained objects. Use them if the layout is simple and static, or if you're annoyed by the Intellisense errors when you don't have the <html> and <body> tags in a standard View.

部分视图更像门户上的web部件——它们是完全自包含的对象。如果布局简单且静态,或者如果在标准视图中没有和标记时,您对智能感知错误感到厌烦,则使用它们。

#2


7  

It works like that:

它的工作原理就像:

  • return View() the view content goes in the @RenderBody() of the /Shared/_Layout.cshtml

    返回View()视图内容位于/Shared/_Layout.cshtml的@RenderBody()中

  • return PartialView() it returns only the view content

    返回PartialView(),它只返回视图内容

#3


3  

Views are the general result of a page that results in a display. It's the highest level container except the masterpage. While a partial view is for a small piece of content that may be reused on different pages, or multiple times in a page.

视图是导致显示的页面的一般结果。它是除主页之外的*别容器。而部分视图是指可以在不同页面上重用或在页面中多次重用的一小部分内容。

If you're coming from webforms, view is similar to a web content form, while a partial view is like a user control.

如果您来自webforms,视图类似于web内容表单,而部分视图类似于用户控件。

#4


3  

Look at *.com site: Main site (View) contains components like:

查看*.com网站:Main site (View)包含以下组件:

  • Tags
  • 标签
  • Related
  • 相关的
  • Ad
  • 广告

So Tags, related, Ad etc. can be composed as PartialViews. The advantage of this is that PartialViews can be simply cached by OutputCache instead of recreating all site: performance gain.

所以标签,相关的,广告等可以组成部分视图。这样做的好处是可以通过OutputCache简单地缓存部分视图,而不是重新创建所有的站点:性能增益。

#5


2  

Consider a partialview like a control in webforms, the idea is the partial is reusable

考虑像webforms中的控件那样的局部视图,其思想是局部视图是可重用的

#6


2  

If you come from a webforms background, think of PartialView as a usercontrol.

如果您来自webforms的背景,可以将PartialView看作用户控件。

#1


38  

In theory, the answer is: A partial view is a "sub-view" that you embed within a main view - something that you might reuse across multiple views, like a sidebar.

理论上,答案是:部分视图是一个“子视图”,您将它嵌入到主视图中——您可以跨多个视图重用它,比如侧边栏。

In practice, the answer is: Very little.

实际上,答案是:非常少。

In theory, partial views are more lightweight than standard views, but it's perfectly OK to pass a "regular" view to RenderPartial and the performance seems to be exactly the same. I frequently use regular .aspx views as "partial" views because you can make them reference a master view in order to provide templated content like what you can do with UserControls in ASP.NET WebForms. See here.

理论上,部分视图比标准视图更轻量级,但是将“常规”视图传递给RenderPartial是完全可以的,而且性能似乎完全相同。我经常使用常规的.aspx视图作为“部分”视图,因为您可以使它们引用一个主视图,以便提供模板化的内容,就像您可以在ASP中使用UserControls一样。净WebForms。在这里看到的。

Partial views are more like web parts on a portal - they are completely self-contained objects. Use them if the layout is simple and static, or if you're annoyed by the Intellisense errors when you don't have the <html> and <body> tags in a standard View.

部分视图更像门户上的web部件——它们是完全自包含的对象。如果布局简单且静态,或者如果在标准视图中没有和标记时,您对智能感知错误感到厌烦,则使用它们。

#2


7  

It works like that:

它的工作原理就像:

  • return View() the view content goes in the @RenderBody() of the /Shared/_Layout.cshtml

    返回View()视图内容位于/Shared/_Layout.cshtml的@RenderBody()中

  • return PartialView() it returns only the view content

    返回PartialView(),它只返回视图内容

#3


3  

Views are the general result of a page that results in a display. It's the highest level container except the masterpage. While a partial view is for a small piece of content that may be reused on different pages, or multiple times in a page.

视图是导致显示的页面的一般结果。它是除主页之外的*别容器。而部分视图是指可以在不同页面上重用或在页面中多次重用的一小部分内容。

If you're coming from webforms, view is similar to a web content form, while a partial view is like a user control.

如果您来自webforms,视图类似于web内容表单,而部分视图类似于用户控件。

#4


3  

Look at *.com site: Main site (View) contains components like:

查看*.com网站:Main site (View)包含以下组件:

  • Tags
  • 标签
  • Related
  • 相关的
  • Ad
  • 广告

So Tags, related, Ad etc. can be composed as PartialViews. The advantage of this is that PartialViews can be simply cached by OutputCache instead of recreating all site: performance gain.

所以标签,相关的,广告等可以组成部分视图。这样做的好处是可以通过OutputCache简单地缓存部分视图,而不是重新创建所有的站点:性能增益。

#5


2  

Consider a partialview like a control in webforms, the idea is the partial is reusable

考虑像webforms中的控件那样的局部视图,其思想是局部视图是可重用的

#6


2  

If you come from a webforms background, think of PartialView as a usercontrol.

如果您来自webforms的背景,可以将PartialView看作用户控件。