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.
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);
}
}
Emmmanuel FIADUFEPosted Apr 18, 2023, 10:51 PM
I tried to create company model and staffmodel separately and join the method in companyStaffModelView but is still not working, kindly assist please
Emmmanuel FIADUFEPosted Apr 18, 2023, 5:06 PM
With the update query that is . SelectMany (x=>x.tblStaffs.Select(d=> new StaffManagementModel, the error message says MasterMVCPOS.Models.Staff does not contain a definition for tblStaffs, and I tried to include public virtual ICollection tblStaffs {get; set; } in the model and the error still remains.
Emmmanuel FIADUFEPosted Apr 18, 2023, 4:45 PM
Hello Team,
Thank you all for your help, but after changing the code according to Vishal is still not working yet, I will appreciate if you could look at the code again for me please
Tuhin PaulPosted Apr 18, 2023, 7:39 AM
In the upStaffDetails function, you are making an AJAX request to the UpStaffDetail action in your controller. It looks like you are passing in a StaffId and Department parameter, but you are not using the Department parameter in your action. Your action is returning a JSON object, but I'm not sure if this is the correct data that you need to populate the fields in your modal popup.
In the DeleteStaff function, you are using the sweetAlert library to confirm if the user wants to delete the staff member. If the user confirms, you are making an AJAX request to the DeleteStaff action in your controller, passing in the StaffId parameter. I don't see where you are actually deleting the staff member from the database in your DeleteStaff action. It looks like you are just removing the staff member from the tblStaffs navigation property of a company, but not actually deleting it from the tblStaffs table in the database.
Vishal JoshiPosted Apr 17, 2023, 6:26 AM
Hello
You need to do below changes to get staff details to update.
To Delete staff you need to do below changes in home controller DeleteStaff Method
Thanks
Vishal
Emmmanuel FIADUFEPosted Apr 12, 2023, 12:23 AM
Please it didn't work for me, I wish you could do the correction in the code for me. Thank you
Emmmanuel FIADUFEPosted Apr 11, 2023, 3:46 PM
I will check it out and revert Swain. Thank you for your response
Rajkiran SwainPosted Apr 11, 2023, 6:40 AM
On the provided code, there are a few things that need to be addressed in order to update both the parent and child tables using modal popup:
The
UpStaffDetailaction in the controller only returns one object (i.e.,modifiedData) which contains the details of a single staff member. However, in the JavaScript code, it appears that there are multiple staff members that are being pushed into thetblStaffsarray. This could be a possible source of error.The
DeleteStaffaction in the controller only receives a singleStaffManagementModelobject as a parameter. However, in the JavaScript code, theStaffIdis being passed as a separate parameter. This could also be a possible source of error.It is unclear how the parent table (i.e.,
tblCompanies) is being updated. The provided code only shows the details of a single staff member being retrieved from the database and displayed in the modal popup. However, there is no code that updates the parent table based on any changes made in the modal popup.To update both the parent and child tables using modal popup, you need to modify your code as follows:
Modify the
UpStaffDetailaction in the controller to return all the details of the staff member and their associated company. This can be achieved by removing theFirstOrDefault()method and returning the entiredataListinstead.Modify the
upStaffDetailsfunction in JavaScript to iterate over thedataListand push all the staff members and their associated companies into thetblStaffsarray.Modify the
DeleteStaffaction in the controller to receive theStaffIdas a parameter and delete the corresponding staff member and their associated company from the database.Add code to update the parent table (i.e.,
tblCompanies) based on any changes made in the modal popup. This can be achieved by adding a separateHttpPostaction in the controller that receives the details of the staff member and their associated company and updates the database accordingly.