ASP.NET MVC ViewResult vs PartialViewResult

Viewed 48840

What is the difference between the controller result named ViewResult and PartialViewResult? More importantly, when is the PartialViewResult used?

5 Answers

PartialViewResult is used to render a partialview (fx. just a user control). This is pretty nifty for AJAX stuff, i.e.

<script type="text/javascript">
    $.get(
        "/MyController/MyAction",
        null,
        function (data) { $("#target").html(data) }
     );
</script>

and action

public ActionResult MyAction() 
{
    return PartialView("SomeView");
}

where SomeView is a MVC User Control, e.g.:

<div>
   <%= DateTime.Now.ToString() %>
</div>

http://msmvps.com/blogs/luisabreu/archive/2008/09/16/the-mvc-platform-action-result-views.aspx

In practice, you’ll use the PartialViewResult for outputing a small part of a view. That’s why you don’t have the master page options when dealing with them. On the other hand, you’ll use the ViewResult for getting a “complete” view. As you might expect, the Controller class exposes several methods that will let you reduce the ammount of typing needed for instanting these types of action results.

Generally speaking, ViewResult is for rendering a page with optional master, and PartialViewResult is used for user controls (likely responding to an AJAX request).

Related