Monday, 8 February 2016

What are the different types of results in MVC

Action Result Return type
In the following list we can see the action result type



1.   ViewResult :  This basically render a specific View for the response stream. The View result basically is a class which is inherited by the abstract class of the “ ViewResultBase”, and it is used to render a view. This “ViewResultBase” class contains the methods for finding the specific view for rendering  and also for execution the results. It also contain the properties for indentifying the view to rendering for the application.
public ViewResult Index()
{
    return
 View("ViewResult");  // here It is returning a  View
}

2.   PartialViewResult :- This class also inherited from the abstract base class “ ViewResultBase” class and  it is used to rendering a partial view. This class contains the methods from finding the partial view for rendering and also for the execution of the result. This also contains the properties of  identify the partial view to rendering the application. This is not  like action result. Basically  Partial views are not like the primary thing for display the user which is the view.
public PartialViewResult Index()
{
    return
 PartialView("PartialViewData");
}

3.   RedirectResult: - It is used to perform the HTTP redirect to a given URL from user side. This class is inherited from the Action result abstract class.
public ActionResult Index()
{
    return
 Redirect("http://www.google.com/");
public
 RedirectResult Index1()
{
    return
 Redirect("http://www.google.com/");
}

4.    RedirectToRouteResult :-  This is used to redirect  using specified route data dictionary. This class is inherited from the ActionResult class.
public ActionResult Index(int? Id)
{
    return
 new RedirectToRouteResult(new System.Web.Routing.RouteValueDictionary(new { controller = "Home", action = "List", Id = new int?() }));
}

5.   ContentResult : -  this is used to return an action plain text this is also inherited from actionResult  abstract class.
public ActionResult Index()
{
    return
 Content("Hello");
public
 ContentResult ContentResultIndex()
{
    return
 Content("Hello");
}

6.   JsonResult : -  Action method on controllers basically return the JsonResult means (JavaScript Object Notation result) which can be used in AJAX application. This class inherited from the ActionResult abstract class. Here Json is provided one argument which must be serializable. 
public JsonResult Index()
{
    return
 Json("Hello My Friend!");
}

ActionResult
Helper Method
Description
ViewResult
View
Renders a view as a web page
PartialViewResult
PartialView
Section of a view,that can be rendered inside another view
RedirectResult
Redirect
Redirect to another action method
RedirectToRouteResult
RedirectToRoute
Redirect to another action method
ContentResult
Content
Returns a user-defined content type
JsonResult
Json
Retuns a serialized JSON object
JavaScriptResult
JavaScript
Returns a script that can be executed on the client
FileResult
File
Returns a binary output to write to the response
EmptyResult
(None)
returns a null result

No comments:

Post a Comment

C# program Selection Sorting

Selection sort is a straightforward sorting algorithm. This algorithm search for the smallest number in the elements array and then swap i...