Hermosa Mt

Hermosa Mt

  • NA
  • 44
  • 519

CRUD function for multiple models unable to get fully function MVC

May 28 2021 7:40 AM
Dear all,

I try to run my CRUD application for multiple model of Admin information on datatables using JavaScript + EF MVC but not luck to see result successfully.

I am surfing net , read many the forum and also follow step from youtuber but i m
hang for this two function "Add" and "Edit" on few weeks.

Please help me to check and solve which coding part i m wrong and also check jquery plugin too . Thank you for advanced.

What I have tried:

1) "Add New" function cannot back to same screen when pressed "Submit" button.

2) "Edit" function cannot display out information when pressed "Edit"as pencil icon button
 
Below is my Coding:
Model parts: 
  1. public partial class ADMIN  
  2.     {      
  3.         public string ADMIN_ID { get; set; }  
  4.         public string ADMIN_NAME { get; set; }  
  5.         public string ADMIN_PASSWORD { get; set; }  
  6.         public string ADMIN_EMAIL { get; set; }  
  7.         public string ADMIN_STATUS { get; set; }  
  8.         public string ADMIN_COMMENT { get; set; }  
  9.        
  10.         public virtual STATUS STATUS { get; set; }  
  11.     }  
  12.   
  13.   
  14.    public partial class STATUS  
  15.     {  
  16.          
  17.         public string STATUS_ID { get; set; }  
  18.         public string STATUS_DESC { get; set; }  
  19.     }  
view part:
 
  1. @model ABC.Models.ADMIN  
  2.   
  3. @{  
  4.     Layout = null;  
  5. }  
  6.   
  7. @using (Html.BeginForm("StoreOrEdit""Admin", FormMethod.Post, new { onsubmit = "return SubmitForm(this)" }))  
  8. {  
  9.     @*@Html.HiddenFor(model => model.ADMIN_ID)*@  
  10.   
  11.     <table>  
  12.         <tr>  
  13.             <td>@Html.Label("ID"new { @class = "control-label" })</td>  
  14.             <td>@Html.Label(":"new { @class = "control-label" })</td>  
  15.             <td> @Html.EditorFor(model => model.ADMIN_ID, new { htmlAttributes = new { @class = "form-control", @placeholder = "ID" } })</td>  
  16.             @Html.ValidationMessageFor(model => model.ADMIN_ID, ""new { @class = "text-danger" })  
  17.         </tr>  
  18.         <tr>  
  19.             <td>@Html.Label("Name"new { @class = "control-label" })</td>  
  20.             <td>@Html.Label(":"new { @class = "control-label" })</td>  
  21.             <td> @Html.EditorFor(model => model.ADMIN_NAME, new { htmlAttributes = new { @class = "form-control" } })</td>  
  22.             @Html.ValidationMessageFor(model => model.ADMIN_NAME, ""new { @class = "text-danger" })  
  23.         </tr>  
  24.         <tr>  
  25.             <td>@Html.Label("Pasword"new { @class = "control-label" })</td>  
  26.             <td>@Html.Label(":"new { @class = "control-label" })</td>  
  27.             <td> @Html.EditorFor(model => model.ADMIN_PASSWORD, new { htmlAttributes = new { @class = "form-control" } })</td>  
  28.             @Html.ValidationMessageFor(model => model.ADMIN_PASSWORD, ""new { @class = "text-danger" })  
  29.         </tr>  
  30.         <tr>  
  31.             <td>@Html.Label("Email"new { @class = "control-label" })</td>  
  32.             <td>@Html.Label(":"new { @class = "control-label" })</td>  
  33.             <td> @Html.EditorFor(model => model.ADMIN_EMAIL, new { htmlAttributes = new { @class = "form-control", @placeholder = "Email", @style = "width:580px;" } })</td>  
  34.             @Html.ValidationMessageFor(model => model.ADMIN_EMAIL, ""new { @class = "text-danger" })  
  35.         </tr>  
  36.         <tr>  
  37.             <td>@Html.Label("Status"new { @class = "control-label" })</td>  
  38.             <td>@Html.Label(":"new { @class = "control-label" })</td>  
  39.             <td> @Html.EditorFor(model => model.ADMIN_STATUS, new { htmlAttributes = new { @class = "form-control" } })</td>  
  40.             @Html.ValidationMessageFor(model => model.ADMIN_STATUS, ""new { @class = "text-danger" })  
  41.         </tr>  
  42.         <tr>  
  43.             <td>@Html.Label("Comment"new { @class = "control-label" })</td>  
  44.             <td>@Html.Label(":"new { @class = "control-label" })</td>  
  45.             <td> @Html.EditorFor(model => model.ADMIN_COMMENT, new { htmlAttributes = new { @class = "form-control" } })</td>  
  46.   
  47.         </tr>  
  48.     </table>  
  49.   
  50.   
  51.     <div class="form-group">  
  52.   
  53.         <input type="submit" value="Submit" class="btn btn-success" />  
  54.         <input type="reset" value="Reset" class="btn btn-warning" />  
  55.     </div>  
  56. }  
  1. @model IEnumerable<ABC.Models.ADMIN>  
  2.   
  3. @{  
  4.     ViewBag.Title = "Admin Profile";  
  5.     Layout = "~/Views/Shared/_LayoutPage2.cshtml";  
  6. }  
  7.   
  8. <link href="~/Content/dataTables.bootstrap4.min.css" rel="stylesheet" />  
  9. <link href="~/css/font-awesome.min.css" rel="stylesheet" />  
  10. <a class="btn btn-primary" style="margin-bottom:10px" onclick="PopupForm('@Url.Action("StoreOrEdit","Admin")')">class="fa fa-plus">Add New</a>  
  11.   
  12.   
  13. <table id="adminTable" class="table table-striped table-bordered" style="width:100%">  
  14.     <thead>  
  15.         <tr>  
  16.             <th>  
  17.                 @Html.DisplayName("ID")  
  18.             </th>  
  19.             <th>  
  20.                 @Html.DisplayName("Name")  
  21.             </th>  
  22.             <th>  
  23.                 @Html.DisplayName("Password")  
  24.             </th>  
  25.             <th>  
  26.                 @Html.DisplayName("Email")  
  27.             </th>  
  28.             <th>  
  29.                 @Html.DisplayName("Status")  
  30.             </th>  
  31.             <th>  
  32.                 @Html.DisplayName("Comment")  
  33.             </th>  
  34.             <th></th>  
  35.              
  36.         </tr>  
  37.     </thead>  
  38. </table>  
 
 
  1. @section scripts{  
  2.      <script src="~/Scripts/jquery-ui-1.12.1.min.js"></script>  
  3.     <script src="~/Scripts/jquery.dataTables.min.js"></script>  
  4.     <script src="~/Scripts/dataTables.bootstrap4.min.js"></script>  
  5.      
  6.     <script>  
  7.   
  8.         var Popup, dataTable;  
  9.   
  10.         $(document).ready(function () {  
  11.             dataTable = $("#adminTable").DataTable({  
  12.   
  13.                 "ajax": {  
  14.   
  15.                     "url""@Url.Action("GetAdminData", "Admin")",  
  16.                     "type""GET",  
  17.                     "datatype""json"  
  18.                 },  
  19.   
  20.                 "columns": [  
  21.                     { "data""ADMIN_ID" },  
  22.                     { "data""ADMIN_NAME" ,"width":"100px"},  
  23.                      { "data""ADMIN_PASSWORD" },  
  24.                        { "data""ADMIN_EMAIL" },  
  25.                          { "data""STATUS_DESC" },  
  26.                            { "data""ADMIN_COMMENT" },  
  27.                     {  
  28.                         "data""ADMIN_ID""render"function (data) {  
  29.   
  30.                             return "<a class='btn btn-default btn-sm' onclick=PopupForm('@Url.Action("StoreOrEdit","Admin")/" + data + "')>">Edit</a>" +  
  31.                                    " <a class='btn btn-danger btn-sm' style='margin-left:5px' onclick=Delete(" + data + ")>">Delete</a>";  
  32.                         },  
  33.   
  34.                         "orderable"false,  
  35.                         "searchable"false,  
  36.                         "width" : "150px"  
  37.                     }  
  38.   
  39.   
  40.                 ],  
  41.   
  42.                 "language": {  
  43.                     "emptyTable" : "No data found please click on Add New  Button"  
  44.                 }  
  45.   
  46.             });  
  47.         });  
  48.   
  49.         function PopupForm(url) {  
  50.   
  51.             var formDiv = $('<div />');  
  52.             $.get(url)  
  53.                 .done(function (response) {  
  54.   
  55.                     formDiv.html(response);  
  56.                     console.log(response);  
  57.                     Popup = formDiv.dialog({  
  58.   
  59.                         autoOpen : true,  
  60.                         resizable : false,  
  61.                         title : 'Fill Admin Details',  
  62.                         height : 450,  
  63.                         width : 700,  
  64.                         close: function () {  
  65.   
  66.                             Popup.dialog('destroy').remove();  
  67.                         }  
  68.   
  69.                     });  
  70.   
  71.                 });  
  72.         }  
  73.          
  74.   
  75.         function SubmitForm(form) {  
  76.   
  77.             $.validator.unobtrusive.parse(form);  
  78.             if ($(form).valid()) {  
  79.   
  80.             $.ajax({  
  81.                 type: "POST",  
  82.                 url: form.action,  
  83.                 data: $(form).serialize(),  
  84.                 success: function (data) {  
  85.   
  86.                     if (data.success) {  
  87.   
  88.                         Popup.dialog('close');  
  89.                         dataTable.ajax.reload();  
  90.   
  91.                         $.notify(data.message, {  
  92.                             globalPosition: "top center",  
  93.                             className:"success"  
  94.                         })  
  95.   
  96.   
  97.                     }  
  98.                 }  
  99.                 });  
  100.             }  
  101.   
  102.             return false;  
  103.   
  104.         }  
  105.   
  106.     </script>  
  107. }    
 
 
 
 
 Controller part:
 
  1. //Create Method for Insert and Update  
  2.   
  3.         [HttpGet]  
  4.         public ActionResult StoreOrEdit(string adminModel_Id = " ")  
  5.         {  
  6.             if (adminModel_Id == null)  
  7.                 return View(new ADMIN());  
  8.                
  9.             else  
  10.             {  
  11.   
  12.                 var query = (from objAdmin in SqlDbContext.ADMINs  
  13.                              join objStatus in SqlDbContext.STATUS on objAdmin.ADMIN_STATUS equals objStatus.STATUS_ID  
  14.                              select new  
  15.                              {  
  16.                                  ADMIN_ID = objAdmin.ADMIN_ID,  
  17.                                  ADMIN_NAME = objAdmin.ADMIN_NAME,  
  18.                                  ADMIN_PASSWORD = objAdmin.ADMIN_PASSWORD,  
  19.                                  ADMIN_EMAIL = objAdmin.ADMIN_EMAIL,  
  20.                                  STATUS_DESC = objStatus.STATUS_DESC,  
  21.                                  ADMIN_COMMENT = objAdmin.ADMIN_COMMENT  
  22.                              }).FirstOrDefault(x => x.ADMIN_ID == adminModel_Id);  
  23.                 return View(query);  
  24.             }  
  25.         }  
 
  1. [HttpPost]  
  2.         public ActionResult StoreOrEdit(ADMIN adminModel)  
  3.         {  
  4.             try  
  5.             {  
  6.                 if (SqlDbContext.ADMINs.Where(x => x.ADMIN_ID.Equals(adminModel.ADMIN_ID)).Count() < 1)  
  7.                 {  
  8.                     ADMIN adminObj = new ADMIN();  
  9.                     adminObj.ADMIN_ID = adminModel.ADMIN_ID;  
  10.                     adminObj.ADMIN_NAME = adminModel.ADMIN_NAME;  
  11.                     adminObj.ADMIN_EMAIL = adminModel.ADMIN_EMAIL;  
  12.                     adminObj.ADMIN_STATUS = adminModel.ADMIN_STATUS;  
  13.                     adminObj.ADMIN_COMMENT = adminModel.ADMIN_COMMENT;  
  14.                     SqlDbContext.ADMINs.Add(adminModel);  
  15.                     SqlDbContext.SaveChanges();  
  16.                     return Json(new { success = true, message = "Saved Successfully", JsonRequestBehavior.AllowGet });  
  17.                 }  
  18.                 else  
  19.                 {  
  20.                     // db.Entry(adminOb).State = EntityState.Modified;  
  21.                     ADMIN adminObj = SqlDbContext.ADMINs.SingleOrDefault(x => x.ADMIN_STATUS == "01" && x.ADMIN_ID == adminModel.ADMIN_ID);  
  22.                     adminObj.ADMIN_NAME = adminModel.ADMIN_NAME;  
  23.                     adminObj.ADMIN_EMAIL = adminModel.ADMIN_EMAIL;  
  24.                     adminObj.ADMIN_STATUS = adminModel.ADMIN_STATUS;  
  25.                     adminObj.ADMIN_COMMENT = adminModel.ADMIN_COMMENT;  
  26.                     SqlDbContext.SaveChanges();  
  27.                     return Json(new { success = true, message = "Updated Successfully", JsonRequestBehavior.AllowGet });  
  28.                 }  
  29.             }  
  30.             catch (Exception ex)  
  31.             {  
  32.                 Console.WriteLine(ex.InnerException);  
  33.                 throw;  
  34.             }  
  35.   
  36.         }