tags:

views:

261

answers:

3

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.

A: 

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.RenderPartial inside your main view.

Jonas H
+1  A: 

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
mare
+3  A: 

Return View() - Renders an .aspx page

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

Return PartialView() - Renders .ascx 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;
      }
   }
}
Alexander
downvoting for repeating my answer and taking the credit..
mare
i'd downvote if i could, but ReturnView() vs View() has absolutely nothing to do with whether an ".aspx" or ".ascx" view is rendered. CORRECTION -- it appears that it may indeed handle what view is fetched using convention (i.e.: controller renders view of same name). regardless, you can feed both a .aspx and a .ascx view to PartialView() with no problems.
kdawg
`View()` returns a `ViewResult` and PartialView() returns a `PartialViewResult`. Run Reflector on the two methods for yourself and see which one you should use when deciding what to render. `PartialViewResult` and `ViewResult` are two different classes that both inherit from `ViewResultBase`, which are intended for different purposes! PartialViews don't have MasterPages (Views can), are you really going to render "`somecontrol.ascx`" using `View()`...
Alexander