In this article I will explain with an example, how to use the @Html.Action function to render Partial View in ASP.Net MVC Razor.
The data will be fetched from database using Entity Framework and then the Partial View will be rendered using the @Html.Action function in ASP.Net MVC Razor.
Database
This article makes use of the Microsoft’s Northwind Database. The download and install instructions are provided in the link below.
Configuring and connecting Entity Framework to database
Now I will explain the steps to configure and add Entity Framework and also how to connect it with the database.
You will need to add Entity Data Model to your project by right clicking the Solution Explorer and then click on Add and then New Item option of the Context Menu.
From the Add New Item window, select ADO.NET Entity Data Model and set its Name as NorthwindModel and then click Add.
Then the Entity Data Model Wizard will open up where you need to select EF Designer database option.
Now the wizard will ask you to connect and configure the Connection String to the database.
You will need to select the
1. SQL Server Instance
2. Database
And then click Test Connection to make sure all settings are correct.
Once the Connection String is generated, click Next button to move to the next step.
Next you will need to choose the Entity Framework version to be used for connection.
Now you will need to choose the Tables you need to connect and work with Entity Framework. Here Customers Table is selected.
The above was the last step and you should now have the Entity Data Model ready with the Customers Table of the Northwind Database.
Controller
The Controller consists of the following Action method.
Action method for rendering View
Inside this Action method, the Top 10 Customer records are fetched from the Customers Table of the Northwind Database and returned to the View.
Child Action method for rendering Partial View
Inside this Action method, the CustomerId is received as parameter and then using Entity Framework, the record for the specific Customer is fetched and returned to the Partial View.
The Partial Action method is decorated with ChildActionOnly attribute.
The ChildActionOnly attribute is used to make sure that an Action method is only called from inside the View and cannot be access directly by the User.
Generally it is used for Action methods invoked by Partial Views. It is not necessary to have this attribute over a Child Action method. But as a good practice it is recommended.
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
NorthwindEntities entities = new NorthwindEntities();
return View(from customer in entities.Customers.Take(10)
select customer);
}
[ChildActionOnly]
public ActionResult Details(string customerId)
{
NorthwindEntities entities = new NorthwindEntities();
return PartialView("Details", entities.Customers.Find(customerId));
}
}
View
Now you will need to Right Click inside the Controller class and click on the Add View option in order to create a View for the Controller.
The Name of the View is set to Index, the Template option is set to Empty, the Model class is set to Customer Entity (the one we have generated using Entity Framework) and finally the Data context class is set to NorthwindEntities.
Inside the View, in the very first line the Customer Entity is declared as IEnumerable which specifies that it will be available as a Collection.
For displaying the records, an HTML Table is used. A loop will be executed over the Model which will generate the HTML Table rows with the Customer records.
The last column of the HTML Table consists of the @Html.Action function which renders the Partial View. The name of the Action method i.e. Details and its parameter i.e. CustomerId are passed to the @Html.Action function.
@model IEnumerable<PartialView_Action_MVC.Customer>
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width"/>
<title>Index</title>
</head>
<body>
<h4>Customers</h4>
<hr/>
<table cellpadding="0" cellspacing="0" id="CustomerGrid">
<tr>
<th>CustomerID</th>
<th>Contact Name</th>
<th>Address</th>
</tr>
@foreach (Customer customer in Model)
{
<tr>
<td>@customer.CustomerID</td>
<td>@customer.ContactName</td>
<td>@Html.Action("Details", new { CustomerId = customer.CustomerID })</td>
</tr>
}
</table>
</body>
</html>
Partial View
In order to add Partial View, you will need to Right Click inside the Controller class and click on the Add View option in order to create a View for the Controller.
The Name of the View is set to Details, the Template option is set to Empty, the Model class is set to Customer Entity (the one we have generated using Entity Framework), the Data context class is set to NorthwindEntities and finally the Create as a partial view option needs to be checked.
Inside the Partial View, in the very first line the Customer Entity is declared as Model for the Partial View. The details of the Customer is displayed using the Html.DisplayFor helper method.
@model Partial_View_Entity_MVC.Customer
@Html.DisplayFor(model => model.Address)
<br/>
@Html.DisplayFor(model => model.City),
@Html.DisplayFor(model => model.PostalCode)
<br/>
@Html.DisplayFor(model => model.Country)
Screenshot
Downloads