Html.Action And Html.RenderAction In ASP.NET MVC

Introduction


This article introduces both @Html.Action and @Html.RenderAction. These are used to call a partial view in another view by action method. As we have other options such as @Html.Partial and @Html.RenderPartial to call a partial view in other views then why do we use @Html.Action and @Html.RenderAction ? We use these two html helper methods in the following scenarios.

  1. To call partial view in another view.
  2. Partial view is independent to corresponding view in other words partial view model in not related to corresponding view model in strongly typed views.
  3. We need some operations over data of partial view before it render in corresponding view.
  4. We call a partial view from ChildActionOnly action methods in another view by GET request.

Whenever we got above situation in our application then we prefer to use these Html helper methods. Now let’s have a look on summary information of these Html helper methods.

@Html.Action


This Html.Action renders partial view as an HTML string so we can store it in another string variable. It is string return type method so first it returns result as a string then renders result to response.

@Html.RenderAction


This is also same as Html.Action but main difference is that it renders result directly to response that’s why it is more efficient if the action returns a large amount of HTML over @Html.Action.

Now we will have a look on particle implementation of these two.

Using Code

We create an MVC application which has a view for employee login and employee registration as both employee login and employee registration are independent to each other. To combine these on single view, we create two partial views, one for Employee login and another for employee registration. As these two views are also independent to corresponding views in which these call.

view
Figure 1: Partial View in corresponding View

First of all we create two view models one for Employee Login (EmployeeLoginViewModel) and another for Employee Registration (EmployeeViewModel). The following code snippet shows both.

  1. namespace EFOperation.Models  
  2. {  
  3.     public class EmployeeLoginViewModel  
  4.     {  
  5.         public string Email { getset; }  
  6.         public string Password { getset; }  
  7.     }  
  8. }  
  9.   
  10. using System.ComponentModel.DataAnnotations;  
  11.   
  12. namespace EFOperation.Models  
  13. {  
  14.     public class EmployeeViewModel  
  15.     {  
  16.         public string Name { getset; }  
  17.         public string Email { getset; }  
  18.         public string Password { getset; }  
  19.         [Display(Name="Confirm Password")]  
  20.         public string ConfirmPassword { getset; }  
  21.     }  
  22. }  
As per figure 1, we create an Index view which has both employee login and employee registration partial view so we define a controller that has three action methods. The “Index” action method returns main view and other two child action methods “EmployeeLogin” and “EmployeeRegistration”. The following code snippet shows EmployeeController.
  1. using EFOperation.Models;  
  2. using System.Web.Mvc;  
  3.   
  4. namespace EFOperation.Controllers  
  5. {  
  6.     public class EmployeeController : Controller  
  7.     {  
  8.          
  9.         public ActionResult Index()  
  10.         {  
  11.             return View("Index");  
  12.         }  
  13.   
  14.         [ChildActionOnly]  
  15.         public ActionResult EmployeeLogin()  
  16.         {  
  17.             EmployeeLoginViewModel model = new EmployeeLoginViewModel();  
  18.             return PartialView("_EmployeeLogin",model);  
  19.         }  
  20.         [ChildActionOnly]  
  21.         public ActionResult EmployeeRegistration()  
  22.         {  
  23.             EmployeeViewModel model = new EmployeeViewModel();  
  24.             return PartialView("_EmployeeRegistration",model);  
  25.         }  
  26.     }  
  27. }  
In our example, the Index view is nota strongly typed view. If it is strongly typed then it doesn’t have any impact on partial view model because with help of @Html.Action and @Html.RenderAction, we can use independent models in partial views. Now have a look that how partial views are called in corresponding view. The following code snippet for Employee Login partial view.
  1. @model EFOperation.Models.EmployeeLoginViewModel  
  2.   
  3. <section id="loginForm">  
  4.     @using (Html.BeginForm("EmployeeLogin""Employee"new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { @class = "form-horizontal", role = "form" }))  
  5.     {  
  6.         @Html.AntiForgeryToken()  
  7.         <h4>Use a local account to log in.</h4>  
  8.         <hr />  
  9.         <div class="form-group">  
  10.             @Html.LabelFor(m => m.Email, new { @class = "col-md-2 control-label" })  
  11.             <div class="col-md-10">  
  12.                 @Html.TextBoxFor(m => m.Email, new { @class = "form-control" })  
  13.             </div>  
  14.         </div>  
  15.         <div class="form-group">  
  16.             @Html.LabelFor(m => m.Password, new { @class = "col-md-2 control-label" })  
  17.             <div class="col-md-10">  
  18.                 @Html.PasswordFor(m => m.Password, new { @class = "form-control" })  
  19.                 @Html.ValidationMessageFor(m => m.Password)  
  20.             </div>  
  21.         </div>  
  22.         <div class="form-group">  
  23.             <div class="col-md-offset-2 col-md-10">  
  24.                 <input type="submit" value="Log in" class="btn btn-default" />  
  25.             </div>  
  26.         </div>          
  27.     }  
  28. </section>  
The following code snippet for Employee registration view.
  1. @model EFOperation.Models.EmployeeViewModel  
  2.   
  3. @using (Html.BeginForm("EmployeeRegistration""Employee", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))  
  4. {     
  5.     <h4>Create a new account.</h4>  
  6.     <hr />  
  7.      
  8.     <div class="form-group">  
  9.         @Html.LabelFor(m => m.Name, new { @class = "col-md-2 control-label" })  
  10.         <div class="col-md-10">  
  11.             @Html.TextBoxFor(m => m.Name, new { @class = "form-control" })  
  12.         </div>  
  13.     </div>  
  14.     <div class="form-group">  
  15.         @Html.LabelFor(m => m.Email, new { @class = "col-md-2 control-label" })  
  16.         <div class="col-md-10">  
  17.             @Html.TextBoxFor(m => m.Email, new { @class = "form-control" })  
  18.         </div>  
  19.     </div>  
  20.     <div class="form-group">  
  21.         @Html.LabelFor(m => m.Password, new { @class = "col-md-2 control-label" })  
  22.         <div class="col-md-10">  
  23.             @Html.PasswordFor(m => m.Password, new { @class = "form-control" })  
  24.         </div>  
  25.     </div>  
  26.     <div class="form-group">  
  27.         @Html.LabelFor(m => m.ConfirmPassword, new { @class = "col-md-2 control-label" })  
  28.         <div class="col-md-10">  
  29.             @Html.PasswordFor(m => m.ConfirmPassword, new { @class = "form-control" })  
  30.         </div>  
  31.     </div>  
  32.     <div class="form-group">  
  33.         <div class="col-md-offset-2 col-md-10">  
  34.             <input type="submit" class="btn btn-default" value="Register" />  
  35.         </div>  
  36.     </div>  
  37. }  
Now we call both partial views with help of @Html.Action and @Html.RenderAction in main index view. The following code snippet is for the same.
  1. @{  
  2.     ViewBag.Title = "Index";  
  3. }  
  4.   
  5. <div class="row">  
  6.   
  7.     <div class="col-lg-6">  
  8.         @Html.Action("EmployeeLogin")  
  9.     </div>  
  10.     <div class="col-lg-6">  
  11.         @{Html.RenderAction("EmployeeRegistration");}  
  12.     </div>  
  13.    
  14. </div>  
Now run the application and we get the same result as figure 2.

Index view
Figure 2: Index view

Read more articles on ASP.NET,


Similar Articles