Implementing Modal Popup In MVC Application

Introduction 

In this article we will implement modal pop up to display the detailed information of user after clicking on detail anchor. Here I am going to implement modal popup on list of my last article. So this is the view on which we are going to apply modal popup.

View code
  1. Enumerable<CodeFirst.Models.FriendsInfo>  
  2.   
  3. @{  
  4.     ViewBag.Title = "Index";  
  5. }  
  6.   
  7. <h2>Index</h2>  
  8.   
  9. <p>  
  10.     @Html.ActionLink("View All", "Index")  
  11.   
  12.     @using (Html.BeginForm("Search", "Home", FormMethod.Post))  
  13.     {  
  14.         <table>  
  15.             <tr>  
  16.                 <td>  
  17.                     <input type="text" id="txtName" name="searchparam" placeholder="type here to search" />  
  18.                 </td>  
  19.                 <td>  
  20.                     <input type="submit" id="btnSubmit" value="Search" />  
  21.                 </td>  
  22.             </tr>  
  23.         </table>  
  24.     }  
  25.   
  26. </p>  
  27. <table class="table">  
  28.     <tr>  
  29.         <th>  
  30.             @Html.DisplayNameFor(model => model.Name)  
  31.         </th>  
  32.         <th>  
  33.             @Html.DisplayNameFor(model => model.Mobile)  
  34.         </th>  
  35.         <th>  
  36.             @Html.DisplayNameFor(model => model.Address)  
  37.         </th>  
  38.         <th></th>  
  39.     </tr>  
  40.   
  41.     @foreach (var item in Model)  
  42.     {  
  43.         <tr>  
  44.             <td>  
  45.                 @Html.DisplayFor(modelItem => item.Name)  
  46.             </td>  
  47.             <td>  
  48.                 @Html.DisplayFor(modelItem => item.Mobile)  
  49.             </td>  
  50.             <td>  
  51.                 @Html.DisplayFor(modelItem => item.Address)  
  52.             </td>  
  53.             <td>  
  54.                 @*@Html.ActionLink("Edit", "Edit", new { id=item.Id }) |  
  55.                     @Html.ActionLink("Details", "Details", new { id=item.Id }) |  
  56.                     @Html.ActionLink("Delete", "Delete", new { id=item.Id })*@  
  57.   
  58.                 <a href="javascript:void(0);" class="anchorDetail"  data-id="@item.Id">Details</a>  
  59.   
  60.             </td>  
  61.         </tr>  
  62.     }  
  63.   
  64. </table>   
The view looks like the following:
 
 

As we can see we have detail anchor, with class anchorDetail and with data-id, which will get the id of clicked anchor and show the corresponding data to modal (detail view) on screen.

We have an Action method Details(int id) which will return the partial view. 
  1. public ActionResult Details(int Id)  
  2. {  
  3.     FriendsInfo frnds = new FriendsInfo();  
  4.     frnds = db.FriendsInfo.Find(Id);  
  5.     return PartialView("_Details",frnds);  
  6. }   
Here we added a partial view for this purpose to show detail view when user click on detail anchor in the list.
 
View Code 
  1. @model CodeFirst.Models.FriendsInfo  
  2.   
  3. <div>  
  4.     
  5.     <div class="modal-header">  
  6.         <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>  
  7.         <h4 class="modal-title" id="myModalLabel">FriendsInfo</h4>  
  8.     </div>                 
  9.                   
  10.       
  11.     <hr />  
  12.     <dl class="dl-horizontal">  
  13.         <dt>  
  14.             @Html.DisplayNameFor(model => model.Name)  
  15.         </dt>  
  16.   
  17.         <dd>  
  18.             @Html.DisplayFor(model => model.Name)  
  19.         </dd>  
  20.   
  21.         <dt>  
  22.             @Html.DisplayNameFor(model => model.Mobile)  
  23.         </dt>  
  24.   
  25.         <dd>  
  26.             @Html.DisplayFor(model => model.Mobile)  
  27.         </dd>  
  28.   
  29.         <dt>  
  30.             @Html.DisplayNameFor(model => model.Address)  
  31.         </dt>  
  32.   
  33.         <dd>  
  34.             @Html.DisplayFor(model => model.Address)  
  35.         </dd>  
  36.   
  37.     </dl>  
  38. </div>  
We have a div for modal pop-up.
  1. <div id='myModal' class='modal'>  
  2.     <div class="modal-dialog">  
  3.         <div class="modal-content">  
  4.             <div id='myModalContent'></div>  
  5.         </div>  
  6.     </div>   
  7.       
  8. </div>   
Here is the script for showing modal (partial view) on above div when user click on detail anchor. Here we used Ajax call for this purpose.

Script 
  1. @section scripts  
  2. {  
  3.     <script src="~/Scripts/jquery-1.10.2.min.js"></script>  
  4.     <script src="~/Scripts/bootstrap.js"></script>  
  5.     <script src="~/Scripts/bootstrap.min.js"></script>  
  6. <script>  
  7.     var TeamDetailPostBackURL = '/Home/Details';  
  8.     $(function () {  
  9.         $(".anchorDetail").click(function () {  
  10.             debugger;  
  11.             var $buttonClicked = $(this);  
  12.             var id = $buttonClicked.attr('data-id');  
  13.             var options = { "backdrop""static", keyboard: true };  
  14.             $.ajax({  
  15.                 type: "GET",  
  16.                 url: TeamDetailPostBackURL,  
  17.                 contentType: "application/json; charset=utf-8",  
  18.                 data: { "Id": id },  
  19.                 datatype: "json",  
  20.                 success: function (data) {  
  21.                     debugger;  
  22.                     $('#myModalContent').html(data);  
  23.                     $('#myModal').modal(options);  
  24.                     $('#myModal').modal('show');                    
  25.   
  26.                 },  
  27.                 error: function () {  
  28.                     alert("Dynamic content load failed.");  
  29.                 }  
  30.             });  
  31.         });  
  32.         //$("#closebtn").on('click',function(){  
  33.         //    $('#myModal').modal('hide');    
  34.   
  35.         $("#closbtn").click(function () {  
  36.             $('#myModal').modal('hide');  
  37.         });        
  38.     });  
  39.      
  40. </script>  
  41.   
  42. }  
After clicking on detail, here we have the following:
 
 
 
Closure

We have implemented modal pop-up (partial view) successfully.Thanks for reading this article. 

Up Next
    Ebook Download
    View all
    Learn
    View all