In MVC controls there are different type of action result we use as a return type from a controller action method in which ViewResult, ActionResult and JsonResult are commonly used. In this article we will learn what are the available action results and which one we need to use in which case.
The ActionResult class is the base class for action results, following types derive from ActionResult:
ContentResult: Represents a user-defined content type that is the result of an action method.
EmptyResult: Represents a user-defined content type that is the result of an action method.
FileResult: Represents a base class that is used to send binary file content to the response.
HttpUnauthorizedResult: Represents the result of an unauthorized HTTP request.
JavaScriptResult: Sends JavaScript content to the response.
JsonResult: Represents a class that is used to send JSON-formatted content to the response.
RedirectResult: Controls the processing of application actions by redirecting to a specified URI.
RedirectToRouteResult: Represents a result that performs a redirection by using the specified route values dictionary.
ViewResultBase: Represents a base class that is used to provide the model to the view and then render the view to the response.
ViewReulst: it is derived from ViewResultBase class, Represents a class that is used to render a view by using an IView instance that is returned by an IViewEngine object.
PartialViewResult: : it is derived from ViewResultBase class, Represents a base class that is used to send a partial view to the response.
Most of the items are self explanatory, when they need to use. What about ViewResult and ActionResult
Suppose a case where we want to return either ViewResult or JsonResult then what would be the action result type, see the following case
public XXXXXXX Edit(Int32 productId)
{
Product product = GetProduct();
if (productId < 100)
return View(product);
else
return Json(product, JsonRequestBehavior.AllowGet);
}
As you can see if productId < 100 then I want to return the ViewResult otherwise JsonResult,
So what action type should be used for this action method
ActionResult will be used because it is dynamic which can be used for any kind of action result.
![]() |
Having 13+ years of experience in Microsoft Technologies (C#, ASP.Net, MVC and SQL Server). Worked with Metaoption LLC, for more than 9 years and still with the same company. Always ready to learn new technologies and tricks.
|
By Ali Adravi On 19 Jul, 13 Viewed: 3,819 |
Action filter in MVC is from very first version. Sometimes we want to perform logic before or after calling the action method, to support this MVC provides action filters. Action filters are used as attributes. Filter can be applied on action method or directly to the controller. Suppose we want... By Hamden On 13 Jul 2013 Viewed: 1,443
Sometimes we need a thanks page say we have user registration, change password, activate account functionality in our application then we need a thanks page after registering with our site, to say thanks for registering with us or showing confirmation that your password is successfully changed or... By Hamden On 30 Jun 2013 Viewed: 3,793
In MVC, autocomplete with jquery is the best way to pull data from database and render according to our requirements, so in this article we will use jquery to show the auto complete text box without any ajax controls. We need a method to get the data from database, a controller method to handle the... By Ali Adravi On 29 Jun 2013 Viewed: 7,049
Upload multiple files with model data in MVC is really very easy, when I started to test by uploading some files, I though it would be more complicated but it is really not. In my previous post [ASP.Net MVC file upload][1], I promised to post soon about how to upload multiple files. When I was... By Ali Adravi On 04 Jun 2013 Viewed: 25,605
How to upload file or image in asp.net mvc is not difficult but is totally different compare to asp.net FileUpload control. If we are asp.net developer and we are new to mvc then we immediately noticed we don’t have server side controls so the FileUpload control as well and our problem start here.... By Ali Adravi On 25 May 2013 Viewed: 8,102