Thursday, May 17, 2012

Rendering a Child Action

You can invoke a child action using the Html.Action helper. With this helper, the action method is
executed, the ViewResult is processed, and the output is injected into the response to the client.



Below is the example that explain how you can implement child Action
 using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 using System.Web;  
 using System.Web.Mvc;  
 namespace child_Action.Controllers  
 {  
   public class ChildActionController : Controller  
   {  
     //  
     // GET: /ChildAction/  
     public ActionResult Index()  
     {  
       return View();  
     }  
     public ActionResult ChildActionDemo()  
     {  
       return View();  
     }  
     [ChildActionOnly]  
     public ActionResult Time()  
     {  
       return PartialView(DateTime.Now);  
     }  
   }  
 }  
 --------------------------------------------------------------------------------------------------------------  
 View -> ChildAction->ChildActionDemo  
 @{  
 ViewBag.Title = "Child Action Demonstration";  
 }  
 <p>This is the method rendered by the action method</p>  
 @Html.Action("Time")  
 <p>This is the method rendered by the action method again</p>  
 --------------------------------------------------------------------------------------------------------------  
 Partial View: Shared->Time  
 @model DateTime  
 <p>The time is: @Model.ToShortTimeString()</p>  

No comments:

Post a Comment