What is the difference between the controller result named ViewResult
and PartialViewResult
? More importantly, when is the PartialViewResult
used?
ActionResult is an abstract class, and it's base class for ViewResult class. In MVC framework, it uses ActionResult class to reference the object your action method returns. And invokes ExecuteResult method on it. And ViewResult is an implementation for this abstract class.
PartialViewResult is used to return the partial view. Basically, it is a class which implements the ViewResultBase abstract class that used to render partial view. PartialViewResult class inherit from ViewResultBase class.
ViewResult represents a class that is used to render a view by using an IView instance that is returned by an IViewEngine object. View() creates an object that renders a view to the response.
When you set Action's return type ActionResult , you can return any subtype of it e.g Json,PartialView,View,RedirectToAction.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With