Wednesday, August 6, 2014

Child Action Methods in ASP.NET MVC4

Child Action Methods in ASP.NET MVC4




In this article we will explore Child Action method which is accessible at View level. In general each public method in a controller class is an action method.

There could be multiple scenarios when we want to display some dynamic information (data) on pages. Child action is helpful in those scenarios.

ChildAction method is somewhere an action method thought its accessible from View only, if you invoked this action via URL then it prompts you an error which we will look down the level in this article.

I have a HomeController in MVC application which contains a code as given below:

  1: using System;
  2: using System.Collections.Generic;
  3: using System.Linq;
  4: using System.Web;
  5: using System.Web.Mvc;
  6: using FiltersDemo.Controllers.CustomFilters;
  9: namespace FiltersDemo.Controllers
 10: {
 11:     public class HomeController : Controller
 12:     {
 13:         #region Commented
 14:         //[CustomAuthorization]
 15:         //[CustomAction]
 16:         //[CustomResultAttribute]
 17:         //[CustomExceptionAttribute]
 18:         #endregion Commented
 20:         public ActionResult Index()
 21:         {
 24:             ViewBag.TempValue = "Index Action called at HomeController";
 25:             return View();
 26:         }
 28:         [ChildActionOnly]
 29:         public ActionResult ChildAction(string param)
 30:         {
 31:             ViewBag.Message = "Child Action called. "+ param;
 32:             return View();
 33:         }
 35:     }
 36: }


To behave an action as child action it requires decorating with [ChildActionOnly] as shown in image below:



Initially it is invoking to Index action which in turn returning to Index views, and at View level it calls to ChildAction named as “ChildAction”.

This is the code of index.cshtml as shown below:

  1: @{
  2:     ViewBag.Title = "Index";
  3: }
  4: <h2>
  5: Index</h2>
  6: <!DOCTYPE html>
  7: <html>
  8: <head>
  9:     <title>Error</title>
 10: </head>
 11: <body>
 12:     <ul>
 13:        <li>
 14:             @ViewBag.TempValue
 15:         </li>
 16:         <li>@ViewBag.OnExceptionError</li>
 18:         @*<li>@{Html.RenderAction("ChildAction", new { param = "first" });}</li>@**@
 19:         @Html.Action("ChildAction", "Home", new { param= "first" })
 21:     </ul>
 22: </body>
 23: </html>
And this is how we declare an action in view as depicted in image below:


Now I run application and it will call an action method via Index.cshtml page. And result is as below:


Note: if you try to access ChildAction method directly than it prompts you an error as per image depicted below.

Copy this URL and paste it to browser => http://localhost:50255/Home/ChildAction/Second

It prompts you an error:


It may help you sometime in upcoming time.


To know more about MVC please go through with given below link.

MVC Articles
Enjoy Coding and Reading clip_image010

1 comment :

  1. Step1: First create a basic mvc 4 application and a controller named “HomeController” to it.
    Step2: Now add two views named “Index” and “ChildAction” like this”.

    For full implementation refer here: