Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC ViewResult vs PartialViewResult

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

like image 260
Captain Sensible Avatar asked Jan 23 '09 13:01

Captain Sensible


People also ask

What is difference between ActionResult and ViewResult in MVC?

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.

What is PartialViewResult in MVC?

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.

What is the use of ViewResult in MVC?

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.

Can I return ActionResult Instead of view result?

When you set Action's return type ActionResult , you can return any subtype of it e.g Json,PartialView,View,RedirectToAction.


2 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> 
like image 127
veggerby Avatar answered Oct 11 '22 11:10

veggerby


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).

like image 31
anonymous Avatar answered Oct 11 '22 10:10

anonymous