In this article I will explain with an example, how to load Partial View using jQuery AJAX in ASP.Net Core MVC.
The Partial View data will be fetched from database using jQuery AJAX and then it will be rendered as HTML in ASP.Net Core MVC.
Note: For beginners in ASP.Net Core MVC, please refer my article ASP.Net MVC Core Hello World Tutorial with Sample Program example.
 
 
Database
Here I am making use of Microsoft’s Northwind Database. You can download it from here.
 
 
Model
Following class is used as the Model class.
public class Customer
{
    public string CustomerID { get; set; }
    public string ContactName { get; set; }
    public string Address { get; set; }
    public string City { get; set; }
    public string Country { get; set; }
    public string PostalCode { get; set; }
} 
 
 
Entity Data Model
Following is the Database Context class which is used to connect to the Database.
Note: For more information on creating Database Context class using Entity Framework, please refer my article ASP.Net Core: Simple Entity Framework Tutorial with example.
 
using PartialView_EF_Core_MVC.Models;
using Microsoft.EntityFrameworkCore;
 
namespace PartialView_EF_Core_MVC
{
    public class DBCtx : DbContext
    {
        public DBCtx(DbContextOptions<DBCtx> options) : base(options)
        {
        }
 
        public DbSet<Customer> Customers { get; set; }
    }
}
 
 
Controller
The Database Context class is injected into the Controller class using Dependency Injection method.
The Controller consists of the following Action methods.
Action method for handling GET operation
Inside this Action method, the Top 10 Customer records are fetched from the Customers Table of the Northwind Database and returned to the View.
 
Action method for handling jQuery POST operation
This Action method handles the call made from the jQuery AJAX function from the View.
The value of the customerId parameter is used to fetch the Customer record using Entity Framework which is then used to populate the Details Partial View.
Finally, the Partial View is returned from the Controller.
public class HomeController : Controller
{
    private DBCtx Context { get; }
    public HomeController(DBCtx _context)
    {
        this.Context = _context;
    }
 
    public IActionResult Index()
    {
        List<Customer> customers = (from customer in this.Context.Customers.Take(10)
                                    select customer).ToList();
        return View(customers);
    }
 
    [HttpPost]
    public IActionResult Details(string customerId)
    {
        return PartialView("Details", this.Context.Customers.Find(customerId));
    }
}
 
 
View
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 Table records.
The last column of the HTML Table consists of an HTML Anchor Link. The HTML Anchor Link has been assigned a jQuery Click event handler.
When the HTML Anchor Link is clicked, a jQuery AJAX Call is made to the Details Action method of the Controller and the Details Partial View is fetched as HTML which is finally displayed using Bootstrap Modal Popup window.
Note: For more details on how to use jQuery AJAX for calling Controller’s Action method in ASP.Net Core MVC, please refer my article ASP.Net Core: jQuery AJAX and JSON Example in ASP.Net Core MVC.
 
@using PartialView_EF_Core_MVC.Models;
@model IEnumerable<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" class="grid" id="CustomerGrid">
        <tr>
            <th>CustomerID</th>
            <th>Contact Name</th>
            <th>City</th>
            <th>Country</th>
            <th></th>
        </tr>
        @foreach (Customer customer in Model)
        {
            <tr>
                <td>@customer.CustomerID</td>
                <td>@customer.ContactName</td>
                <td>@customer.City</td>
                <td>@customer.Country</td>
                <td><a class="details" href="javascript:;">View</a></td>
            </tr>
        }
    </table>
    <div id="partialModal" class="modal" tabindex="-1" role="dialog">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title">Customer Details Form</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <div class="modal-body">
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                </div>
            </div>
        </div>
    </div>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous" />
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
    <script type="text/javascript">
        $(function () {
            $("#CustomerGrid .details").click(function () {
                var customerId = $(this).closest("tr").find("td").eq(0).html();
                $.ajax({
                    type: "POST",
                    url: "/Home/Details",
                    data: { "customerId": customerId },
                    success: function (response) {
                        $("#partialModal").find(".modal-body").html(response);
                        $("#partialModal").modal('show');
                    },
                    failure: function (response) {
                        alert(response.responseText);
                    },
                    error: function (response) {
                        alert(response.responseText);
                    }
                });
            });
        });
    </script>
</body>
</html>
 
Load Partial View using jQuery AJAX in ASP.Net Core MVC
 
 
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.
Inside the Add New Item dialog, the Razor View – Empty options needs to be selected as shown below.
Load Partial View using jQuery AJAX in ASP.Net Core MVC
 
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 are displayed using the Html.DisplayFor helper method.
@model PartialView_EF_Core_MVC.Models.Customer
 
<table cellpadding="0" cellspacing="0" border="0">
    <tr>
        <td valign="top"><b>@Html.DisplayNameFor(model => model.Address):</b></td>
        <td>
            @Html.DisplayFor(model => model.Address)
            <br/>
            @Html.DisplayFor(model => model.City),
            @Html.DisplayFor(model => model.PostalCode)
            <br/>
            @Html.DisplayFor(model => model.Country)
        </td>
    </tr>
</table>
 
 
Screenshot
Load Partial View using jQuery AJAX in ASP.Net Core MVC
 
 
Downloads