Load partial view using jquery ajax call in asp.net MVC
In this post, We will learn loading partial view using jquery ajax call. you can load multiple numbers of partial views using this method.
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 MVC5 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 one partial View (_GetUserList.cstml) that will be displayed. So I'll show you how easily we can load this partial view viaAjax. It will make the page more intuitive and seamless to users.
At MainView (UserList.cshtml):
- <body>
- <h1>Load Partial View at Parent View</h1>
- <div id="dvPartial">
- </div>
- </body>
Jquery:
- <script src="https://code.jquery.com/jquery-3.5.1.min.js" />
- <script>
- $.get("/Home/GetUserList",null, function(data){
- $("#dvPartial").html(data);
- });
- </script>
Controller:
- public ActionResult GetUserList(){
- List<User> lstUser = db.Users.ToList();
- return PartialView("_GetUserList"lstUser);
- }
Partial View (_GetUserList.cstml):
- @model IEnumerable<MyProject.Models.User>
- <body>
- <div>
- <table style="width: 100%%; height: 100%">
- <tr>
- <th>User Name</th>
- <th>Registered Date</th>
- <th>Date Of Birth</th>
- <th>Is Active</th>
- </tr>
- @foreach (var item in Model)
- {
- <tr>
- <td> @item.Name</td>
- <td> @item.RegisteredDate</td>
- <td> @item.DOB</td>
- <td> @item.IsActive</td>
- </tr>
- }
- </table>
- </div>
- </body>
Comments
Post a Comment