Rahul Patil

Rahul Patil

  • 1.5k
  • 183
  • 31.1k

How To Open BootStrap Model when User Click On Edit Button?

Feb 1 2020 10:01 AM

Database Field Name:

  • empid int pk auto increment
  • empname varchar(50)
  • empsalary varchar(50)
  • empage int 

I want to open a bootstrap model when user click on edit button then open the edit view see my console logs:

plzz help ?

I create an edit partial view see code

Code:

HomeController.cs

  1. public class HomeController : Controller  
  2.     {  
  3.         empEntities _dbcontext = new empEntities();  
  4.   
  5.         public ActionResult Index()  
  6.         {  
  7.            return View(_dbcontext.empls.ToList());
  8.         }  
  9.   
  10.         public ActionResult Edit(int empid)  
  11.         {  
  12.             var id = _dbcontext.empls.Find(empid);  
  13.             return View();  
  14.         }  
  15.   
  16.         [HttpPost]  
  17.         public ActionResult Edit(empl empobj)  
  18.         {  
  19.             var recordfind = _dbcontext.empls.Find(empobj.empid);  
  20.   
  21.             recordfind.empname = empobj.empname;  
  22.             recordfind.empsalary = empobj.empsalary;  
  23.             recordfind.empage = empobj.empage;  
  24.   
  25.             _dbcontext.SaveChanges();  
  26.             return View();  
  27.         }  
  28.     }  
Index.cshtml
  1. @model IEnumerable<DatabasefirstApproach.empl>    
  2.     
  3. <link href="~/Content/PagedList.css" rel="stylesheet" type="text/css" />    
  4. <script src="~/scripts/jquery-3.4.1.min.js"></script>    
  5.     
  6. @{    
  7.     ViewBag.Title = "Index";    
  8. }    
  9.     
  10. <h2>Index</h2>    
  11.     
  12. <p>    
  13.     @Html.ActionLink("Create New""Create")    
  14. </p>    
  15. <table class="table">    
  16.     <tr>    
  17.         <th>    
  18.             @Html.DisplayNameFor(model => model.empname)    
  19.         </th>    
  20.         <th>    
  21.             @Html.DisplayNameFor(model => model.empsalary)    
  22.         </th>    
  23.         <th>    
  24.             @Html.DisplayNameFor(model => model.empage)    
  25.         </th>    
  26.         <th></th>    
  27.     </tr>    
  28.     
  29.     @foreach (var item in Model)    
  30.     {    
  31.         <tr>    
  32.             <td>    
  33.                 @Html.DisplayFor(modelItem => item.empname)    
  34.             </td>    
  35.             <td>    
  36.                 @Html.DisplayFor(modelItem => item.empsalary)    
  37.             </td>    
  38.             <td>    
  39.                 @Html.DisplayFor(modelItem => item.empage)    
  40.             </td>    
  41.             <td>    
  42.                 <button class="btn btn-primary " id="btn-edit-employ" data-id="@item.empid">Edit</button>    
  43.     
  44.                 @Html.ActionLink("Details""Details"new { id = item.empid }) |    
  45.                 @Html.ActionLink("Delete""Delete"new { id = item.empid })    
  46.             </td>    
  47.         </tr>    
  48.     }    
  49.     
  50. </table>    
  51.     
  52. <script>    
  53.         $(document).ready(function () {    
  54.             $('#btn-edit-employ').click(function () {    
  55.                 debugger    
  56.                 var empid = $('#btn-edit-employ').attr('data-id');    
  57.                 var url = 'Home/Edit/' + empid;    
  58.     
  59.                 data: "{empname:'" + $("#item.empname").val() + "',empsalary:'" + $("#item.empsalary").val() + "',empage:'" + $("#item.empage").val() + "'}",    
  60.                 function (data) {    
  61.     
  62.                     $("#item.empname").val(empname);    
  63.                     $("#item.empsalary").val(empsalary);    
  64.                     $("item.empage").val(empage);    
  65.                 }    
  66.                 console.log("document loads 1");    
  67.             });    
  68.             console.log("document loads 2");    
  69.         });    
  70. </script>    
  71. //edit view is a partial view
Edit.cshtml
  1. @model DatabasefirstApproach.empl    
  2.     
  3. @using (Html.BeginForm())    
  4. {    
  5.     @Html.AntiForgeryToken()    
  6.     
  7.     <div @*class="form-horizontal"*@ class="modal fade" id="mod-edit-emp">    
  8.         <h4>empl</h4>    
  9.         <hr />    
  10.         @Html.ValidationSummary(true""new { @class = "text-danger" })    
  11.         @Html.HiddenFor(model => model.empid)    
  12.     
  13.         <div class="form-group">    
  14.             @Html.LabelFor(model => model.empname, htmlAttributes: new { @class = "control-label col-md-2" })    
  15.             <div class="col-md-10">    
  16.                 @Html.EditorFor(model => model.empname, new { htmlAttributes = new { @class = "form-control" } })    
  17.                 @Html.ValidationMessageFor(model => model.empname, ""new { @class = "text-danger" })    
  18.             </div>    
  19.         </div>    
  20.     
  21.         <div class="form-group">    
  22.             @Html.LabelFor(model => model.empsalary, htmlAttributes: new { @class = "control-label col-md-2" })    
  23.             <div class="col-md-10">    
  24.                 @Html.EditorFor(model => model.empsalary, new { htmlAttributes = new { @class = "form-control" } })    
  25.                 @Html.ValidationMessageFor(model => model.empsalary, ""new { @class = "text-danger" })    
  26.             </div>    
  27.         </div>    
  28.     
  29.         <div class="form-group">    
  30.             @Html.LabelFor(model => model.empage, htmlAttributes: new { @class = "control-label col-md-2" })    
  31.             <div class="col-md-10">    
  32.                 @Html.EditorFor(model => model.empage, new { htmlAttributes = new { @class = "form-control" } })    
  33.                 @Html.ValidationMessageFor(model => model.empage, ""new { @class = "text-danger" })    
  34.             </div>    
  35.         </div>    
  36.     
  37.         <div class="form-group">    
  38.             <div class="col-md-offset-2 col-md-10">    
  39.                 <input type="submit" value="Save" class="btn btn-default" />    
  40.             </div>    
  41.         </div>    
  42.     </div>    
  43. }    
  44.     
  45. <div>    
  46.     @Html.ActionLink("Back to List""Index")    
  47. </div>  
I want to open the BootStrap Model When the user click on the edit button??
 
My empid is fetch when I click on Edit Button but my Edit view is Not open??
 
Edit view is a partial view. empid is fetched when I click on edit button but my edit partial view is not open ??
 
see browser logs:
 
 
How to open edit view when click on the edit button?
 
plz help?? 

Answers (1)