return View() vs returnPartialView()

发布时间 2023-05-09 14:19:08作者: ChuckLu

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