Create, Edit, Delete,Details Operation From Fancy Box in MVC5 Application

This article will solve the problem of how to create a MVC 5 application with CURD (Create, Edit, Details, Delete) operation using Fancy Box and Entity Framework.

First we create a model and context class. We create a student information class with the following properties.

  1. [Key]  
  2. public Guid ID { getset; }  
  3. [Required(ErrorMessage = "*")]  
  4. [Display(Name = "First Name")]  
  5. public string FirstName { getset; }  
  6. [Required(ErrorMessage = "*")]  
  7. [Display(Name = "Last Name")]  
  8. public string LastName { getset; }  
  9. [Required(ErrorMessage = "*")]  
  10. [Display(Name = "Course")]  
  11. public string Course { getset; }  
  12. [Required(ErrorMessage = "*")]  
  13. [Display(Name = "Phone No")]  
  14. public string PhoneNo { getset; } 

And second is the context class like this that inherits the DbContext class:

  1. public class DbConnectionContext : DbContext  
  2. {  
  3.     public DbConnectionContext():base("name=dbContext")  
  4.     {  }  
  5.     public DbSet<StudentInformation> Students { getset; }  
  6. } 

We also have a web config file. We configure connectionStrings under the Web.Config file.

  1. <connectionStrings>  
  2.      <add name="dbContext" connectionString="Data Source=localhost;  
  3.          Initial Catalog=CommonDataBase; Integrated Security=true" providerName="System.Data.SqlClient" />    
  4. </connectionStrings> 

Then we create a controller class in a controller folder and edit the name as StudentController. Add Scaffold select MVC 5 Controller with Views, using Entity Framework.

Add Controller

We edit the Index View according to requirements and add the following code:

add Reference
Then we add these references to the Index View:

  1. @model IEnumerable<CURDOperationFromFancybox.Models.StudentInformation>  
  2.  @{  
  3.      ViewBag.Title = "Index";  
  4.  }  
  5.  <h2>Student</h2>  
  6.  <p>  
  7.      <a class="createEditStudent btn btn-primary" data-fancybox-type="iframe" href="@Url.Action("Create", "Student")">Create New Student</a>  
  8.  </p> <table class="table">  
  9.      <tr>  
  10.          <th>  
  11.              @Html.DisplayNameFor(model => model.FirstName)  
  12.          </th>  
  13.          <th>  
  14.              @Html.DisplayNameFor(model => model.LastName)  
  15.          </th>  
  16.          <th>  
  17.              @Html.DisplayNameFor(model => model.Course)  
  18.          </th>  
  19.          <th>  
  20.              @Html.DisplayNameFor(model => model.PhoneNo)  
  21.          </th>  
  22.          <th></th>  
  23.      </tr>  
  24.  @foreach (var item in Model) {  
  25.      <tr>  
  26.          <td>  
  27.              @Html.DisplayFor(modelItem => item.FirstName)  
  28.          </td>  
  29.          <td>  
  30.              @Html.DisplayFor(modelItem => item.LastName)  
  31.          </td>  
  32.          <td>  
  33.              @Html.DisplayFor(modelItem => item.Course)  
  34.          </td>  
  35.          <td>  
  36.              @Html.DisplayFor(modelItem => item.PhoneNo)  
  37.          </td>  
  38.          <td>             
  39.            <a class="createEditStudent btn btn-primary" data-fancybox-type="iframe"                                                href="@Url.Action("Edit", "Student", new {id=item.ID })">Edit</a>  
  40.   
  41.               <a class="createEditStudent btn btn-primary" data-fancybox-type="iframe"   
  42.              href="@Url.Action("Details", "Student", new { id = item.ID })">Details</a>  
  43.   
  44.              <a class="createEditStudent btn btn-danger" data-fancybox-type="iframe"    
  45.               href="@Url.Action("Delete", "Student", new {id=item.ID })">Delete</a>  
  46.          </td>  
  47.      </tr>  
  48.  }  
  49. </table> 
And Add jQuery code to Index View and .createEditStudent as in the following:
  1. <script type="text/javascript">  
  2.     $(document).ready(function () {  
  3.         $('.createEditStudent').fancybox({  
  4.             fitToView: false,  
  5.             width: '600px',  
  6.             height: '400px',  
  7.             autoSize: false,  
  8.             closeClick: false,  
  9.             openEffect: 'none',  
  10.             closeEffect: 'none',  
  11.             padding: 0,  
  12.             closeBtn: false,  
  13.             'afterClose'function () {  
  14.                 window.location.reload();  
  15.             },   
  16.         });  
  17.     });  
  18. </script> 
We add a Create View in the Student folder, Create.cshtml. The purpose of this view is to create a new entry stored in the database.
  1. @model CURDOperationFromFancybox.Models.StudentInformation  
  2. @{  
  3.     ViewBag.Title = "Create";  
  4.     Layout = null;  
  5. }   
  6. <html>  
  7. <head>  
  8.     <title></title>  
  9.     <script src="~/Scripts/jquery-1.5.1.min.js"></script>  
  10.     <script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>  
  11.     <script src="~/Scripts/jquery.validate.min.js"></script>  
  12.     <script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>  
  13.     <link href="~/Content/bootstrap.min.css" rel="stylesheet" />  
  14.     <link href="~/Content/bootstrap.css" rel="stylesheet" />   
  15.     <style type="text/css">  
  16.         .table-style   
  17.         {  
  18.             margin-left: 150px;  
  19.         }  
  20.         .table-style tr td   
  21.         {  
  22.             height: 50px;  
  23.         }  
  24.     </style>  
  25.     <script type="text/javascript">  
  26.         function OnSuccess(response)   
  27.         {  
  28.             if (response == "success")   
  29.             {  
  30.                 parent.jQuery.fancybox.close();  
  31.             }   
  32.             else   
  33.             {  
  34.                 alert("Error. Please check out");  
  35.             }  
  36.         }  
  37.     </script>  
  38. </head>  
  39. <body style="background-color:#fff;">  
  40.     <div style="height: 30px; width: 599.4px; background-color: #558cc3; border: 0px solid red; text-align: center;">  
  41.         <b style="color: white;"> Create New Student</b>  
  42.         <div style="float:right; height:30px; width:30px; border-left:1px solid #c8c8c8">   
  43.         <a href="javascript:parent.jQuery.fancybox.close();" style="color: orange; cursor: pointer; text-decoration: none; ">  
  44.          <i style="font-size:20px; text-decoration:none">X</i></a>  
  45.         </div>  
  46.     </div>   
  47.     <br /><br />  
  48.     @using (Ajax.BeginForm("Create"new AjaxOptions { HttpMethod = "POST", OnSuccess = "OnSuccess" }))  
  49.     {  
  50.         @Html.AntiForgeryToken()  
  51.         <table class="table-style">  
  52.             <tr>  
  53.                 <td>@Html.TextBoxFor(model => model.FirstName, new { @class = "form- control",   
  54.                     @style = "width:300px", placeholder = "First Name" })</td>  
  55.                 <td style="color:red">@Html.ValidationMessageFor(model => model.FirstName)</td>  
  56.             </tr>  
  57.             <tr>  
  58.                 <td>@Html.TextBoxFor(model => model.LastName, new { @class = "form-control",   
  59.                   @style = "width:300px", placeholder = "Last Name" })</td>  
  60.                 <td style="color:red">@Html.ValidationMessageFor(model => model.LastName)</td>  
  61.             </tr>  
  62.             <tr>  
  63.                 <td>@Html.TextBoxFor(model => model.Course, new { @class = "form-control",   
  64.                   @style = "width:300px", placeholder = "Course" })</td>  
  65.                 <td style="color:red">@Html.ValidationMessageFor(model => model.Course)</td>  
  66.             </tr>  
  67.             <tr>  
  68.                 <td>@Html.TextBoxFor(model => model.PhoneNo, new { @class = "form-control",   
  69.                   @style = "width:300px", placeholder = "Phone Number" })</td>  
  70.                 <td style="color:red">@Html.ValidationMessageFor(model => model.PhoneNo)</td>  
  71.             </tr>  
  72.         </table>   
  73.         <br />  
  74.         <div class="form-group" style="margin-left:140px;">  
  75.             <div class="col-md-offset-2 col-md-10">  
  76.                 <input type="submit" id="btnSubmit" value="Create" class="btn btn-primary" />  
  77.                              
  78.            <a class="btn btn-danger" href="javascript:parent.jQuery.fancybox.close();"   
  79.                   style="cursor: pointer; text-decoration: none; ">Close</a>  
  80.             </div>  
  81.         </div>  
  82.     }  
  83. </body>  
  84. </html> 
Create New Form
 
We need to submit a form from the Ajax.BeginForm and add the OnSuccess attribute. The purposes of these are for the Ajax.BeginForm close of the fancy box after submitting the form successfully.

We add an edit view in the Student folder, edit.cshtml. The purpose is to update the record in the stored record.

  1. @model CURDOperationFromFancybox.Models.StudentInformation  
  2.  @{  
  3.      ViewBag.Title = "Edit";  
  4.      Layout = null;  
  5.  }  
  6.  <html>  
  7.  <head>  
  8.      <title></title>  
  9.      <script src="~/Scripts/jquery-1.5.1.min.js"></script>  
  10.      <script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>  
  11.      <script src="~/Scripts/jquery.validate.min.js"></script>  
  12.      <script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>  
  13.      <link href="~/Content/bootstrap.min.css" rel="stylesheet" />  
  14.      <link href="~/Content/bootstrap.css" rel="stylesheet" />  
  15.      <style type="text/css">  
  16.          .table-style {  
  17.              margin-left: 150px;  
  18.          }  
  19.              .table-style tr td {  
  20.                  height: 50px;  
  21.              }  
  22.      </style>  
  23.      <script type="text/javascript">  
  24.          function OnSuccess(response) {  
  25.              if (response == "success") {  
  26.                  parent.jQuery.fancybox.close();  
  27.              } else {  
  28.                  alert("Error. Please check out");  
  29.              }  
  30.          }  
  31.      </script>  
  32.  </head>  
  33.  <body style="background-color:#fff;">  
  34.      <div style="height: 30px; width: 599.4px; background-color: #558cc3; border: 0px solid #b200ff; text-align: center;">  
  35.          <b style="color: white;"> Update Student</b>  
  36.          <div style="float:right; height:30px; width:30px; border-left:1px solid #c8c8c8">  
  37.              <a href="javascript:parent.jQuery.fancybox.close();" style="color: #000000; cursor: pointer; text-decoration: none; ">  
  38.   
  39.               <i style="font-size:20px; text-decoration:none">X</i></a>  
  40.          </div>  
  41.      </div>  
  42.      <br /><br />  
  43.      @using (Ajax.BeginForm("Edit"new AjaxOptions { HttpMethod = "POST", OnSuccess = "OnSuccess" }))  
  44.      {  
  45.          @Html.HiddenFor(m => m.ID)  
  46.          @Html.AntiForgeryToken()  
  47.          <table class="table-style">  
  48.              <tr>  
  49.                  <td>@Html.TextBoxFor(model => model.FirstName, new { @class = "form-control", @style = "width:300px", placeholder = "First Name" })</td>  
  50.                  <td style="color:red">@Html.ValidationMessageFor(model => model.FirstName)</td>  
  51.              </tr>  
  52.              <tr>  
  53.                  <td>@Html.TextBoxFor(model => model.LastName, new { @class = "form-control",   
  54.                      @style = "width:300px", placeholder = "Last Name" })</td>  
  55.                  <td style="color:red">@Html.ValidationMessageFor(model => model.LastName)</td>  
  56.              </tr>  
  57.              <tr>  
  58.                  <td>@Html.TextBoxFor(model => model.Course, new { @class = "form-control",  
  59.                 @style = "width:300px", placeholder = "Course" })</td>  
  60.                  <td style="color:red">@Html.ValidationMessageFor(model => model.Course)</td>  
  61.              </tr>  
  62.              <tr>  
  63.                  <td>@Html.TextBoxFor(model => model.PhoneNo, new { @class = "form-control",   
  64.                      @style = "width:300px", placeholder = "Phone Number" })</td>  
  65.                  <td style="color:red">@Html.ValidationMessageFor(model => model.PhoneNo)</td>  
  66.              </tr>  
  67.          </table>  
  68.          <br />  
  69.          <div class="form-group" style="margin-left:140px;">  
  70.              <div class="col-md-offset-2 col-md-10">  
  71.                  <input type="submit" value="Update" class="btn btn-primary" />  
  72.                                 
  73.                  <a class="btn btn-danger" href="javascript:parent.jQuery.fancybox.close();"   
  74.                   style="cursor: pointer; text-decoration: none; ">Close</a>  
  75.              </div>  
  76.          </div>  
  77.      }  
  78.  </body>  
  79. </html> 
Update
 
We add a delete view in the Student folder, delete.cshtml. This purpose is to delete a record in the stored record.
  1. @model CURDOperationFromFancybox.Models.StudentInformation  
  2.  @{  
  3.      ViewBag.Title = "Delete";  
  4.      Layout = null;  
  5.  }  
  6.  <html>  
  7.  <head>  
  8.      <title></title>  
  9.      <script src="~/Scripts/jquery-1.5.1.min.js"></script>  
  10.      <script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>  
  11.      <script src="~/Scripts/jquery.validate.min.js"></script>  
  12.      <script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>  
  13.      <link href="~/Content/bootstrap.min.css" rel="stylesheet" />  
  14.      <link href="~/Content/bootstrap.css" rel="stylesheet" />  
  15.      <style type="text/css">  
  16.          .table-style {  
  17.              margin-left: 150px;  
  18.          }  
  19.              .table-style tr td {  
  20.                  height: 30px;  
  21.              }  
  22.      </style>  
  23.      <script type="text/javascript">  
  24.          function OnSuccess(response) {  
  25.              if (response == "success") {  
  26.                  parent.jQuery.fancybox.close();  
  27.              } else {  
  28.                  alert("Error. Please check out");  
  29.              }  
  30.          }  
  31.      </script>  
  32.  </head>  
  33.  <body style="background-color:#fff;">  
  34.      <div style="height: 30px; width: 599.4px; background-color: #558cc3; border: 0px solid red; text-align: center;">  
  35.          <b style="color: white;">Are you sure to delete this student</b>  
  36.          <div style="float:right; height:30px; width:30px; border-left:1px solid #c8c8c8">  
  37.              <a href="javascript:parent.jQuery.fancybox.close();" style="color: red; cursor: pointer; text-decoration: none; ">  
  38.                <i style="font-size:20px; text-decoration:none">X</i></a>  
  39.          </div>  
  40.      </div><br />  
  41.      <table class="table-style">  
  42.          <tr>  
  43.              <td>@Html.DisplayNameFor(model => model.FirstName):   </td>  
  44.              <td>@Html.DisplayFor(model => model.FirstName)</td>  
  45.          </tr>  
  46.          <tr>  
  47.              <td>@Html.DisplayNameFor(model => model.LastName):   </td>  
  48.              <td>@Html.DisplayFor(model => model.LastName)</td>  
  49.    
  50.         </tr>  
  51.          <tr>  
  52.              <td>@Html.DisplayNameFor(model => model.Course):   </td>  
  53.              <td>@Html.DisplayFor(model => model.Course)</td>  
  54.          </tr>  
  55.          <tr>  
  56.              <td>@Html.DisplayNameFor(model => model.PhoneNo):   </td>  
  57.              <td>@Html.DisplayFor(model => model.PhoneNo)</td>  
  58.          </tr>  
  59.          <tr>  
  60.              <td></td>  
  61.              <td></td>  
  62.          </tr>  
  63.          <tr>  
  64.              <td colspan="2">  
  65.                  @using (Ajax.BeginForm("Delete"new AjaxOptions { HttpMethod = "POST", OnSuccess = "OnSuccess" }))  
  66.                  {  
  67.                      @Html.AntiForgeryToken()  
  68.                      <div class="form-actions no-color">  
  69.                          <input type="submit" value="Delete" class="btn btn-primary" />  
  70.                                       
  71.                          <a class="btn btn-danger" href="javascript:parent.jQuery.fancybox.close();"   
  72.                            style="cursor: pointer; text-decoration: none; ">Close</a>  
  73.                      </div>  
  74.                  }  
  75.              </td>  
  76.          </tr>  
  77.      </table>  
  78.  </body>  
  79. </html>
delete

We add a detail view to the Student folder, details.cshtml. This purpose is to see the details of a record in an existing record.
  1. @model CURDOperationFromFancybox.Models.StudentInformation  
  2. @{  
  3.     ViewBag.Title = "Delete";  
  4.     Layout = null;  
  5. }  
  6. <html>  
  7. <head>  
  8.     <title></title>  
  9.     <script src="~/Scripts/jquery-1.5.1.min.js"></script>  
  10.     <script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>  
  11.     <script src="~/Scripts/jquery.validate.min.js"></script>  
  12.     <script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>  
  13.     <link href="~/Content/bootstrap.min.css" rel="stylesheet" />  
  14.     <link href="~/Content/bootstrap.css" rel="stylesheet" />  
  15.     <style type="text/css">  
  16.         .table-style {  
  17.             margin-left: 150px;  
  18.         }  
  19.    
  20.             .table-style tr td {  
  21.                 height: 30px;  
  22.             }  
  23.     </style>  
  24.     <script type="text/javascript">  
  25.         function OnSuccess(response) {  
  26.             if (response == "success") {  
  27.                 parent.jQuery.fancybox.close();  
  28.             } else {  
  29.                 alert("Error. Please check out");  
  30.             }  
  31.         }  
  32.     </script>  
  33.    
  34. </head>  
  35. <body style="background-color:#fff;">  
  36.     <div style="height: 30px; width: 599.4px; background-color: #558cc3; border: 0px solid red; text-align: center;">  
  37.         <b style="color: white;">Are you sure to delete this student</b>  
  38.         <div style="float:right; height:30px; width:30px; border-left:1px solid #c8c8c8">  
  39.             <a href="javascript:parent.jQuery.fancybox.close();" style="color: red; cursor: pointer; text-decoration: none; ">  
  40.                <i style="font-size:20px; text-decoration:none">X</i></a>  
  41.         </div>  
  42.     </div><br />  
  43.     <table class="table-style">  
  44.         <tr>  
  45.             <td>@Html.DisplayNameFor(model => model.FirstName):   </td>  
  46.             <td>@Html.DisplayFor(model => model.FirstName)</td>  
  47.         </tr>  
  48.    
  49.         <tr>  
  50.             <td>@Html.DisplayNameFor(model => model.LastName):   </td>  
  51.             <td>@Html.DisplayFor(model => model.LastName)</td>  
  52.         </tr>  
  53.         <tr>  
  54.             <td>@Html.DisplayNameFor(model => model.Course):   </td>  
  55.             <td>@Html.DisplayFor(model => model.Course)</td>  
  56.         </tr>  
  57.         <tr>  
  58.             <td>@Html.DisplayNameFor(model => model.PhoneNo):   </td>  
  59.             <td>@Html.DisplayFor(model => model.PhoneNo)</td>  
  60.         </tr>  
  61.         <tr>  
  62.             <td></td>  
  63.             <td></td>  
  64.         </tr>  
  65.         <tr>  
  66.             <td colspan="2">  
  67.                 @using (Ajax.BeginForm("Delete"new AjaxOptions { HttpMethod = "POST", OnSuccess = "OnSuccess" }))  
  68.                 {  
  69.                     @Html.AntiForgeryToken()  
  70.                     <div class="form-actions no-color">  
  71.                         <input type="submit" value="Delete" class="btn btn-primary" />  
  72.                                       
  73.                         <a class="btn btn-danger" href="javascript:parent.jQuery.fancybox.close();"   
  74.                            style="cursor: pointer; text-decoration: none; ">Close</a>  
  75.                     </div>  
  76.                 }  
  77.             </td>  
  78.         </tr>  
  79.     </table>  
  80. </body>  
  81. </html> 
Detail

I have finished the model and Create, Edit, Delete, Details, Index View. Now we will explain the controller class, StudentController.cs

  1. [RoutePrefix("StudentInformation")]  
  2. public class StudentController : Controller  
  3. {  
  4.     private DbConnectionContext db = new DbConnectionContext();  
  5.     [Route("Index")]  
  6.     public ActionResult Index()  
  7.     {  
  8.         return View(db.Students.ToList());  
  9.     }   
  10.     public ActionResult Details(Guid? id)  
  11.     {  
  12.         if (id == null)  
  13.         {  
  14.             return new HttpStatusCodeResult(HttpStatusCode.BadRequest);  
  15.         }  
  16.         StudentInformation studentinformation = db.Students.Find(id);  
  17.         if (studentinformation == null)  
  18.         {  
  19.             return HttpNotFound();  
  20.         }  
  21.         return View(studentinformation);  
  22.     }   
  23.     public ActionResult Create()  
  24.     {  
  25.         return View();  
  26.     }   
  27.     [HttpPost]  
  28.     [ValidateAntiForgeryToken]  
  29.     public ActionResult Create([Bind(Include = "ID,FirstName,LastName,Course,PhoneNo")] StudentInformation studentinformation)  
  30.     {  
  31.         if (ModelState.IsValid)  
  32.         {  
  33.             studentinformation.ID = Guid.NewGuid();  
  34.             db.Students.Add(studentinformation);  
  35.             db.SaveChanges();  
  36.             return Content("success");  
  37.         }  
  38.         return View(studentinformation);  
  39.     }   
  40.     public ActionResult Edit(Guid? id)  
  41.     {  
  42.         if (id == null)  
  43.         {  
  44.             return new HttpStatusCodeResult(HttpStatusCode.BadRequest);  
  45.         }  
  46.         StudentInformation studentinformation = db.Students.Find(id);  
  47.         if (studentinformation == null)  
  48.         {  
  49.             return HttpNotFound();  
  50.         }  
  51.         return View(studentinformation);  
  52.     }   
  53.   
  54.     [HttpPost]        [ValidateAntiForgeryToken]  
  55.     public ActionResult Edit([Bind(Include = "ID,FirstName,LastName,Course,PhoneNo")] StudentInformation studentinformation)  
  56.     {  
  57.         if (ModelState.IsValid)  
  58.         {  
  59.             db.Entry(studentinformation).State = EntityState.Modified;  
  60.             db.SaveChanges();  
  61.             return Content("success")  
  62.         }  
  63.         return View(studentinformation);  
  64.     }   
  65.     public ActionResult Delete(Guid? id)  
  66.     {  
  67.         if (id == null)  
  68.         {  
  69.             return new HttpStatusCodeResult(HttpStatusCode.BadRequest);  
  70.         }  
  71.         StudentInformation studentinformation = db.Students.Find(id);  
  72.         if (studentinformation == null)  
  73.         {  
  74.             return HttpNotFound();  
  75.         }  
  76.         return View(studentinformation);  
  77.     }   
  78.     [HttpPost, ActionName("Delete")]  
  79.     [ValidateAntiForgeryToken]  
  80.     public ActionResult DeleteConfirmed(Guid id)  
  81.     {  
  82.        StudentInformation studentinformation = db.Students.Find(id);  
  83.         db.Students.Remove(studentinformation);  
  84.         db.SaveChanges();  
  85.         return Content("success");  
  86.     }  
  87.     protected override void Dispose(bool disposing)  
  88.     {  
  89.         if (disposing)  
  90.         {  
  91.             db.Dispose();  
  92.         }  
  93.         base.Dispose(disposing);  
  94.     }  
  95. }   
If you have any problem with this code then you can download the code from this link:

https://onedrive.live.com/redir?resid=1F9E67EA808B03D8%21276


Similar Articles