Monday 14 December 2020

ActionResult In MVC5

ActionResult in MVC 5 :

 

In this article we will discuss returning Data from Action method to view. Will focus more on ActionResult. ActionResult is type used to return data from MVC action method to view. Method/Action method can return any data type like int, string, void(nothing), custom data type (ActionResult,ViewResult, ..etc).

 

ActionResult is base type for few custom types like ViewResult, JsonResult, FileResult, RedirectResult, RedirectToRoute etc.

 



Same way other classes like RedirectResult, FileResult,..etc also inherit ActionResult abstract class. 

Let’s create a simple MVC5 application to understand the concept. I have created MVC application



I'm using existing MVC5 project from previous article of ViewLayout you can name your created project whatever you want. Go to controllers in newly created project and Open HomeController.cs file. Create new action method returning string message from MVC action method shown below. We can use any return type like int, string, float, double or custom type based on requirement.

 



1. ViewResult : ViewResult is used to return View with basic data (HTML and C# code available in .cshtml file).

Both highlighted below action method ‘Test’ will return same result when you call controller from URL ‘localhost:#####/Home/Test’. At a time only one test method will execute as both action method with same name will give an error comment second one with View Result type and execute first one. Same while executing second action method, first one should be commented.



 

2.PartialView: Partial view is view used as part of Main view. Partial view can’t use directly but with View. To implement dynamic data based on requirement MVC provide partial view and based on different scenario few partial views created.

Create partial view test method. Partial action method can not called directly but from View.




Above action method can be written with ActionResult also

   public PartialViewResult TestPartial()

   {

       return PartialView("TestPartial");

   }

OR

   public ActionResult TestPartial()

   {

        return PartialView("TestPartial");

   }

 

 

3.JsonResult: This is again custom type used to return Json data from action method to view.

 

    public ActionResult Test()

    {

         string output = "Hello from JsonResult";

         return Json(output, JsonRequestBehavior.AllowGet);

    }

OR

     public JsonResult Test()

     {

          string output = "Hello from JsonResult";

          return Json(output, JsonRequestBehavior.AllowGet);

     }

 

 

4.ContentResult: ContentResult type returns different contents (html, JavaScript, etc) result from action method.

 

 //public ActionResult Test()

 public ContentResult Test()

        {

            return Content("<h2>Hello From ContentResult</h2>");

        }


 

5.FileResult: This type is used to return file(html,text,xml,..etc) from MVC action method to browser.

 

       public ActionResult Test()

//public FileResult Test()

       {

 

            return File(@"D:\test.txt", "text/html");

       }

 

6.RedirectResult: This type is used to return redirect from one view to another view in MVC. Here controller name not mentioned in Redirect method if both view are in same controller. For requested view in different controller

        

 

       //public ActionResult Test()

        public RedirectResult Test()

        {

            return Redirect("About");

        }

 

       // for different controller

        public RedirectResult Test()

        {

            return Redirect("Employee/Index");

        }

 

7.RedirectToAction: This type is used to redirect based on given action method and controller name. If you type url ‘http://localhost:59974/Home/About’ you will automatically redirect to about page.

 

//public ActionResult Test()

        public RedirectToRouteResult Test()

        {

            return RedirectToAction("About");

            //return RedirectToAction("ActionName","ControllerName");

        }

            


  

There are few more types like JavascriptResult, EmptyResult and http status results, which I’m not covered in this article, but if you want you can explore using below URL from Microsoft.

 

DifferentTypes in ActionResult

 

Conclusion : ActionResult is abstract class and it is base class for all ViewResult,PartialViewResult,FileResult, ... etc. 



0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home