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.
3 Answers
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;
}
}
}
-
4i'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.– kdawgMay 17, 2010 at 18:28
-
3
View()
returns aViewResult
and PartialView() returns aPartialViewResult
. Run Reflector on the two methods for yourself and see which one you should use when deciding what to render.PartialViewResult
andViewResult
are two different classes that both inherit fromViewResultBase
, which are intended for different purposes! PartialViews don't have MasterPages (Views can), are you really going to render "somecontrol.ascx
" usingView()
...– AlexMay 17, 2010 at 19:12 -
1@xandercoded all these answers and comments can be replaced by your
PartialViews don't have MasterPages
– mohasOct 6, 2018 at 17:00 -
could be great if someone could explain the reflector code differences in simple words– sergeJan 22, 2021 at 0:55
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
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.