Hello Team,
In my angularJs controller data is not deleting from the database.
$scope.DeleteInvoiceList = function(SalesId,SalesDetailId){
// Need to implement
swal({
title: 'Do you want to delete Record?',
showCancelButton: true,
showConfirmButton: true,
confirmButtonText: 'Yes Delete',
cancelButtonText: 'No pls cancel',
confirmButtonClass: 'btn btn-success',
cancelButtonClass: 'btn btn-danger',
type: 'warning',
buttonsStyling: false
}).then(
function (isConfirm) {
// Called if you click Yes.
if (isConfirm) {
// Make Ajax call.
$.get("/Home/DeleteInvoiceList", { SalesDetailId: SalesDetailId, SalesId:SalesId}, function (res) {
debugger;
if (res) {
dataTable.ajax.reload();
}
})
swal('Confirm', '1 Record deleted successfully', 'success');
//dataTable.ajax.reload();
}
}, function (no) {
// Called if you click No.
if (no == 'cancel') {
swal('Cancelled', '', 'error');
}
});
}
HOME CONTROLLER
public ActionResult DeleteInvoiceList(int SalesDetailId, int SalesId, int ProductId = 0)
{
try
{
ASPNETMASTERPOSTEntities db = new ASPNETMASTERPOSTEntities();
var salesDetails = db.tblSalesDetails.Where(a => a.SalesDetailId == SalesDetailId).ToList();
var sales = db.tblSales.Where(a => a.SalesId == SalesId).FirstOrDefault();
if (salesDetails != null)
db.tblSalesDetails.Remove(item);
if (sales != null)
db.tblSales.Remove(sales);
db.SaveChanges();
return Json(true, JsonRequestBehavior.AllowGet);
}
catch (Exception)
{
return Json(false, JsonRequestBehavior.AllowGet);
}
}
Vishal JoshiPosted Jun 30, 2023, 6:40 AM
Hello Emma,
Please check SalesDetailId passed adequately. in this case, it really not required to pass SalesDetailId we can pass SalesId and based on that we can get SalesDetails and delete all sales detail items.
AngularJs Code to change
Thanks
Emmmanuel FIADUFEPosted Jun 30, 2023, 9:30 AM
Thank you Vishal and Deepak for swift response, it working now
Deepak RawatPosted Jun 29, 2023, 5:59 PM
based on the code you provided, there are a few issues that might prevent the data from being deleted successfully.
salesDetailsas a list of items, but later, you're trying to remove a singleitem. It seems like there's a mismatch between the variable names. You should iterate oversalesDetailsand remove each item individually.After removing the sales details and sales, you need to call
SaveChangeson thedbcontext to persist the changes to the database.In your client-side code, you're using
$.getto make the AJAX call. However, it's generally recommended to use AngularJS's built-in$httpservice for making AJAX requests. Replace the$.getcall with$http.getas follows:By making these changes, your code should properly delete the data from the database when the
DeleteInvoiceListfunction is called in your AngularJS controller.