In this article, we'll discuss partial views in ASP.NET MVC and loading them via jQuery AJAX. There could be several scenarios for this. Let's assume that we have a page that contains multiple partial views and data from various sources. So we can load each partial view using AJAX individually, it will improve the user experience because the components that can be loaded earlier won't be delayed until all the components load. As soon as each control loads, they will be available to the user on the screen.
I have used ASP.NET MVC3 for this article but it does not matter whether you use MVC3 or MVC4 or MVC5. I have created a main View (called here HomePage.cshtml) and created two Partial Views (_ProductDetails.cstml and _UserDetails.cshtml) that will be displayed. So I'll show you how easily we can load these controls viaAjax. It will make the page more intuitive and seamless to users.
I am showing simple data in these controls and one control display the details of the users and other control displays product details. For this, I have created two models, User and Product.
Also, I have created two methods GetUserDetails and GetProductDetails in Home Controller. GetUserDetails returns a list of users and GetProductDetails returns a list of products.
While we can load each control easily via jQuery AJAX. For this jQuery must be included in the on the View. By default it is included in the __Layout.cshtml. It works as a master layout of the page but if you are not using it in your View then include the jQuery file specifically. So my HomePage.cshtml looks like:
- <body>
- <h1>First Partial View</h1>
- <div id="dvUserdetails" style="width: 50%%; height: 130px; display:none">
- </div>
- <h1>Second Partial View</h1>
- <div id="dvProductDetails" style="width: 50%; height: 130px; display:none">
- </div>
- </body>
- <script type="text/javascript">
- $.ajax ({
- url: '/ Home/GetUserDetails',
- contentType: 'application/html; charset=utf-8',
- type: 'GET',
- dataType: 'html'
- })
- .success (function (result) {
- $('#dvUserdetails').html(result);
- })
- .error(function (xhr, status) {
- alert(status);
- })
- $.ajax ({
- url: '/ Home/GetProductDetails',
- contentType: 'application/html; charset=utf-8',
- type: 'GET' ,
- dataType: 'html'
- })
- .success (function (result) {
- $('#dvProductDetails').html(result);
- })
- .error(function (xhr, status) {
- alert(status) ;
- })
- </script>
Also here we can easily pass the parameter to the controller methods if we want, via URL itself. Two partial Views are,
Product partial view ( _ProductDetails.cshtml) is as,
- <body>
- <div>
- <table style="width: 100%%; height: 100%">
- <tr>
- <th>Product Name</th>
- <th>ManufacturedDate</th>
- <th>Price</th>
- <th>IsAvailable</th>
- </tr>
- @foreach (var item in Model)
- {
- <tr>
- <td> @item.Name</td>
- <td> @item.ManufacturedDate</td>
- <td> @item.Price</td>
- <td> @item.IsAvailable</td>
- </tr>
- }
- </table>
- </div>
- </body>
- <body>
- <div>
- <table style="width: 100%; height: 100%%">
- <tr>
- <th>FirstName</th>
- <th>LastName</th>
- <th>DisplayName</th>
- <th>Age</th>
- <th>Profession</th>
- </tr>
- @foreach (var item in Model)
- {
- <tr>
- <td> @item.FirstName</td>
- <td> @item.LastName</td>
- <td> @item.DisplayName</td>
- <td> @item.Age</td>
- <td> @item.Profession</td>
- </tr>
- }
- </table>
- </div>
- </body>
When we'll run the application, it looks like,

These controls load individually via AJAX.

gampa dugPosted Feb 28, 2024, 11:47 PM
Uncaught TypeError TypeError: $.ajax(...).success is not a function
gampa dugPosted Feb 28, 2024, 4:40 PM
Where is the controller code? How is the data being sent to the jquery?
gampa dugPosted Feb 28, 2024, 4:24 PM
It would be prudent for the author to use some indentation in the code samples. It is difficult to quickly ascertain where one function ends and another begins.
Endpoint ExpensesPosted Jan 19, 2023, 2:50 PM
Can someone tell how the partial page is called from ajax? we don't have any link in above ajax code.
Endpoint ExpensesPosted Jan 19, 2023, 2:48 PM
Please someone can explain how ajax is calling particular partial view ?in above code
Balasubramanian RPosted Aug 30, 2022, 9:35 AM
Where is action methods code for GetUserDetails,GetProductDetails?
suv suvPosted Jun 14, 2022, 7:51 AM
$('#dvProductDetails').html(result); this line is giving XSS issues by static code analyzer tools like Fortify. What to do ?
siddharth khatriPosted Oct 6, 2021, 7:56 AM
Thanks sir ,you solved my problem
Dharmendra Kumar PanditPosted Aug 12, 2020, 10:02 PM
Thank you sir. Its helped me alot. Very very thank you.
Jhon HernándezPosted Jul 7, 2020, 8:17 AM
Please leave a link to the code
Shakir AliPosted Mar 28, 2019, 2:33 AM
Hi, You can impliment same code which is given above by using Action in a simple way as given below , It's work for me Public PartialViewResult ProductionModelDetail(int id) { ProductBAL obj = new ProductBAL(); List<ProductionStockDetailModel> productModellist = new List<ProductionStockDetailModel>(); List<ProductionStockDetailEntity> paintentity = obj.ProductionModelDetail(id); ProductionStockDetailModel productModel; if (paintentity != null) { foreach (ProductionStockDetailEntity item in paintentity) { productModel = new ProductionStockDetailModel(); productModel.MakeId = item.MakeId; productModel.ModelId = item.ModelId; productModel.PartId = item.PartId; productModellist.Add(productModel); } } return PartialView(productModellist); }
MrnamsPosted Oct 10, 2018, 12:16 AM
Hello experts,I want to implement load more button for my website ,https://mrnams.com Can any one please give idea how to implement load more button to load more videos without refreshing (I mean without loosing loaded videos) page. Similar to YouTube functionality. I am using .net core 2.1 version Can any one please give idea how to implement load more button to load more videos without refreshing (I mean without loosing loaded videos) page. Similar to YouTube functionality. I am using .net core 2.1 version
Tridip BhattacharjeePosted Dec 11, 2017, 3:30 AM
This code is incomplete. GetUserDetails and GetProductDetails code has not been posted here. this is very odd.
Jean SamsonPosted Jun 19, 2017, 6:53 AM
Nope. Didn't work.
neal tyreePosted Feb 23, 2017, 5:31 PM
I made an account to say thanks.I have looked at so many different ways to populate multiple partials, this is awesome. This is easy to follow and straight forward. I cannot thank you enough. Thank you, thank you, thank you.
kiran kPosted Jan 6, 2017, 4:55 AM
You saved my day.Thank you very much sir!
kiran kPosted Jan 6, 2017, 4:54 AM
Saved my day.Thank you very much sir!
Munesh SharmaPosted May 28, 2016, 2:54 AM
nice
Thokozani GwilizaPosted May 7, 2016, 9:59 AM
Would have been great to see how your controller look like.
Upendra Pratap ShahiPosted Nov 3, 2015, 9:24 AM
good..
Harshal KhatriPosted Sep 23, 2015, 8:02 AM
What if I have Create Partial View that needs to be POST and check the validation and bring success or failure
Karim SouissiPosted Aug 14, 2014, 9:51 AM
Hi Katja , good question , Normally the view might have two Input button wish their Id must be called in the top of the Ajax Implementation, like that : $('#btnAjax').click(function ajaxCall() { //Ajax method });
Katja HansenPosted Apr 1, 2014, 8:55 AM
How do the view know which partialView to use?