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

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

CheapASPNETHostingReview.com | Best and cheap ASP.NET MVC 6 hosting. 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 summery 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.

web2

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.

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.

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.

Get ASPHostPortal 15% discount with this promotional link

The following code snippet for Employee registration view.

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.

Now run the application and we get the same  result as figure 2.

wwwwww