asp.net-mvc “返回 View()”和“返回 PartialView()”有什么区别

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

What's the difference between "return View()" and "return PartialView()"

asp.net-mvc

提问by Shay Friedman

I understand that partial views are used to render parts of a view. But I can't understand what's the difference between return View()and return PartialView()and when do you use each one.

我知道部分视图用于渲染视图的一部分。但我不明白你使用每一个之间有什么区别return View()以及什么return PartialView()时候使用。

采纳答案by xandercoded

Return View()- Renders an .aspx|.cshtml page

返回View()- 呈现一个 .aspx|.cshtml 页面

  • Renders a normal .aspx page that can also contain Partial Views
  • 呈现一个普通的 .aspx 页面,该页面也可以包含部分视图

Return PartialView()- Renders .ascx|.cshtml Control

返回PartialView()- 呈现 .ascx|.cshtml 控件

  • Renders a segment of HTML to the browser that can be requested through AJAX or Non-AJAX requests alike.
  • 将一段 HTML 呈现给浏览器,可以通过 AJAX 或非 AJAX 请求等方式进行请求。

View() returns ViewResultPartialView() returns PartialViewResultboth inherit from ViewResultBase

View() 返回ViewResultPartialView() 返回PartialViewResult都继承自ViewResultBase

The difference is described by Reflectorbelow...

不同之处由反射器描述如下...

public class PartialViewResult : ViewResultBase
{
   // Methods
   protected override ViewEngineResult FindView(ControllerContext context)
   {
      ViewEngineResult result = base.ViewEngineCollection.FindPartialView(context, base.ViewName);
      if (result.View != null)
      {
         return result;
      }
      StringBuilder builder = new StringBuilder();
      foreach (string str in result.SearchedLocations)
      {
         builder.AppendLine();
         builder.Append(str);
      }
      throw new InvalidOperationException(string.Format(CultureInfo.CurrentUICulture, MvcResources.Common_PartialViewNotFound, new object[] { base.ViewName, builder }));
   }
}


public class ViewResult : ViewResultBase
{
   // Fields
   private string _masterName;

   // Methods
   protected override ViewEngineResult FindView(ControllerContext context)
   {
      ViewEngineResult result = base.ViewEngineCollection.FindView(context, base.ViewName, this.MasterName);
      if (result.View != null)
      {
         return result;
      }
      StringBuilder builder = new StringBuilder();
      foreach (string str in result.SearchedLocations)
      {
         builder.AppendLine();
         builder.Append(str);
      }
      throw new InvalidOperationException(string.Format(CultureInfo.CurrentUICulture, MvcResources.Common_ViewNotFound, new object[] { base.ViewName, builder }));
   }

   // Properties
   public string MasterName
   {
      get
      {
         return (this._masterName ?? string.Empty);
      }
      set
      {
         this._masterName = value;
      }
   }
}

回答by mare

return PartialView() returns HTML code fragment and it is used with ViewUserControls - ASCX files. The main advantage of using "return PartialView()" is when you don't want to render all the other HTML page stuff, like HTML, BODY, HEAD tags.

return PartialView() 返回 HTML 代码片段,它与 ViewUserControls - ASCX 文件一起使用。使用“return PartialView()”的主要优点是当您不想呈现所有其他 HTML 页面内容时,例如 HTML、BODY、HEAD 标签。

One of the most common uses by me is when I want to render just the user control based on whether the request to an action is AJAX call.

我最常见的用途之一是当我只想根据对操作的请求是否为 AJAX 调用来呈现用户控件时。

So I have a View called MyControl.aspx where I use RenderPartial HTML helper and I have a partial View named MyControl.ascx where I do the actual render.

所以我有一个名为 MyControl.aspx 的视图,我在其中使用了 RenderPartial HTML 帮助程序,我有一个名为 MyControl.ascx 的部分视图,我在其中执行实际渲染。

I can switch between those two in my controller action like this:

我可以在我的控制器操作中在这两者之间切换,如下所示:

if (Request.IsAjaxRequest())
    return PartialView("MyControl"); // this renders MyControl.ascx

return View(); // this render MyControl.aspx

回答by Jonas H?gh

A controller action typically returns a PartialView when AJAX is used, and an update of the page region represented by the partial view is performed. The normal way to use partial views is simply to call Html.RenderPartialinside your main view.

当使用 AJAX 时,控制器操作通常会返回 PartialView,并执行由部分视图表示的页面区域的更新。使用部分视图的正常方法是Html.RenderPartial在主视图中调用。