Hello Team,
Kindly assist me, I have parent table and child table, and am using modal popup to update existing record in dataTable through controller but the modal popup is not able to fetch the data and update the dataTable and delete data as well, though am able to save new data. alright.
<script> function upStaffDetails($StaffId) { var tblStaffs = []; var company = new Object(); var staff = new Object(); $("#staffId").val($StaffId); if ($StaffId != 0) { jQuery.ajax({ url: "@Url.Action("UpStaffDetail", "Home")" + "?StaffId=" + $StaffId + "&Department=abc", data:({ company: company }), type: "Get", dataType: "json", contentType: "application/json; charset=utf-8", success: function (result) { company: company console.log(result); if (result != null) { $("#staffId").val(staff.StaffId); $("#companyId").val(company.CompanyId); $("#date").val(company.DateJoin); $("#department").val(company.Department); $("#jobTilte").val(company.JobTitle); $("#image").val(staff.Picture); $("#staffNo").val(staff.StaffNo); $("#fName").val(staff.FName); $("#lName").val(staff.LName); $("#birthDate").val(staff.BirthDate); $("#phone").val(staff.PhoneNo); $("#email").val(staff.Email); $("#firstName").val(staff.FirstName); $("#lastName").val(staff.LastName); $("#phonenumber").val(staff.PhoneNumb); tblStaffs.push(staff); company.tblStaffs = tblStaffs; } } }); } else { $("#staffId").val(""); $("#picture").val(""); $("#staffNo").val(""); $("#date").val(""); $("#fname").val(""); $("#lname").val(""); $("#birthdate").val(""); $("#phone").val(""); $("#email").val(""); $("#firstname").val(""); $("#lastname").val(""); $("#phonenumb").val(""); } $("#StaffModal").modal('show') } </script>
function DeleteStaff(StaffId) { swal({ title: 'Do you want to delete staff?', showCancelButton: true, confirmButtonColor: "#DD6B55", confirmButtonText: 'Yes Delete', cancelButtonText: 'No pls cancel', confirmButtonClass: 'btn btn-success', cancelButtonClass: 'btn btn-danger', type: "warning", closeOnConfirm: false, closeOnCancel: false }).then( function (isConfirm) { // Called if you click Yes. if (isConfirm) { // Make Ajax call. $.get("/Home/DeleteStaff", { StaffId: StaffId }, function (res) { if (res) { } }) swal('Confirm', '1 staff deleted successfully', 'success'); dataTable.ajax.reload(); } }, function (no) { // Called if you click No. if (no == 'cancel') { swal('Cancelled', '', 'error'); } }); }
CONTROLLER
[HttpGet] public ActionResult UpStaffDetail() { using (var db = new ASPNETMASTERPOSTEntities()) { var dataList = db.tblCompanies.Include("tblStaffs").ToList(); var modifiedData = dataList.AsEnumerable().SelectMany(x => x.tblStaffs.Select(d => new StaffManagementModel { CompanyId = x.CompanyId, DateJoin = x.DateJoin, Department = x.Department, JobTitle = x.JobTitle, StaffId = d.StaffId, Picture = d.Picture, StaffNo = d.StaffNo, FName = d.FName, LName = d.LName, BirthDate = d.BirthDate, PhoneNo = d.PhoneNo, Email = d.Email, FirstName = d.FirstName, LastName = d.LastName, PhoneNumb = d.PhoneNumb })).FirstOrDefault(); return Json(modifiedData, JsonRequestBehavior.AllowGet); } } [HttpPost] public ActionResult DeleteStaff(StaffManagementModel staff) { try { ASPNETMASTERPOSTEntities db = new ASPNETMASTERPOSTEntities(); var Staff = db.tblStaffs.Where(x => x.StaffId == staff.StaffId).FirstOrDefault(); db.tblStaffs.Remove(Staff); db.SaveChanges(); return Json(true, JsonRequestBehavior.AllowGet); } catch (Exception) { return Json(false, JsonRequestBehavior.AllowGet); } }