return View() vs returnPartialView()

return View() vs returnPartialView()

In ASP.NET MVC, both return View() and return PartialView() are used to return a view result from a controller action method.

return View() renders the full HTML layout, including the layout page, while return PartialView() renders only the HTML for the view itself. return PartialView() is typically used when the view is intended to be inserted into another page, or when you only want to update a portion of the page using Ajax.

For example, if you have a view that shows a list of products and you want to use Ajax to update the list without refreshing the whole page, you might use return PartialView() to render the updated product list HTML, which can be inserted into the page using JavaScript. On the other hand, if you want to render a full page with header, footer and other layout elements, you would use return View().

In summary, return View() is used to render a full view including layout, while return PartialView() is used to render just the HTML for the view itself, which is typically used when using Ajax or updating only a portion of the page.

 

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

回答1

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.

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.

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.

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

 

回答2

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

  • Renders a normal .aspx page that can also contain Partial Views

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

  • Renders a segment of HTML to the browser that can be requested through AJAX or Non-AJAX requests alike.

View() returns ViewResult PartialView() returns PartialViewResult both inherit from ViewResultBase

The difference is described by Reflector below...

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;
      }
   }
}

 

作者:Chuck Lu    GitHub    
posted @   ChuckLu  阅读(42)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2021-05-09 凤凰之谜 1/4 潜行者
2021-05-09 凤凰之谜 4/4 猎人
2019-05-09 Google recaptcha在webform中的使用
2018-05-09 lightbox2
2017-05-09 ASP.NET IIS Registration Tool (Aspnet_regiis.exe)
点击右上角即可分享
微信分享提示