asp.net-mvc ASP.NET MVC 中的 View 和 PartialView 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2043394/
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:02:09  来源:igfitidea点击:

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

asp.net-mvcviews

提问by Ben Aston

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

ASP.NET MVC 中的 View 和 PartialView 有什么区别?

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

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

回答by Aaronaught

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 RenderPartialand 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.NET WebForms 中使用 UserControls 做的那样。见这里

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 部件 - 它们是完全独立的对象。如果布局简单且静态,或者当您在标准视图中没有<html><body>标签时对 Intellisense 错误感到恼火,请使用它们。

回答by ThiagoVBDiniz

It works like that:

它是这样工作的:

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

  • return PartialView() it returns only the view content

  • 返回 View() 视图内容进入 /Shared/_Layout.cshtml 的 @RenderBody()

  • return PartialView() 它只返回视图内容

回答by Tim Hoolihan

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.

视图是导致显示的页面的一般结果。它是除 masterpage 之外的最高级别的容器。而局部视图是针对一小段内容,可以在不同的页面上重复使用,或者在一个页面中多次重复使用。

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

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

回答by LiamB

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

如果您来自 webforms 背景,请将 PartialView 视为用户控件。

回答by Dariusz

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

查看 StackOverflow.com 站点:主站点(视图)包含以下组件:

  • 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.

所以标签、相关、广告​​等可以组合为 PartialViews。这样做的好处是 PartialViews 可以简单地由 OutputCache 缓存,而不是重新创建所有站点:性能增益。

回答by Israfel

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

考虑像 webforms 中的控件一样的局部视图,这个想法是局部是可重用的