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 JoshiRank #377 · 6.7K reputationPosted Jun 30, 2023, 2:40 AM EDT
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.
public ActionResult DeleteInvoiceList(int SalesId) { try { ASPNETMASTERPOSTEntities db = new ASPNETMASTERPOSTEntities(); var salesDetails = db.tblSalesDetails.Where(a => a.SalesId== SalesId).ToList(); var sales = db.tblSales.Where(a => a.SalesId == SalesId).FirstOrDefault(); if (salesDetails != null){ foreach(var item in salesDetails) 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); } }AngularJs Code to change
$.get("/Home/DeleteInvoiceList", { SalesId:SalesId}, function (res) { if (res) { dataTable.ajax.reload(); }Thanks
Deepak RawatRank #646 · 3.7K reputationPosted Jun 29, 2023, 1:59 PM EDT
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.foreach (var item in salesDetails) { db.tblSalesDetails.Remove(item); }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:$http.get("/Home/DeleteInvoiceList", { params: { SalesDetailId: SalesDetailId, SalesId: SalesId } }) .then(function (response) { // Handle the response if (response.data) { dataTable.ajax.reload(); } }) .catch(function (error) { // Handle errors });By making these changes, your code should properly delete the data from the database when the
DeleteInvoiceListfunction is called in your AngularJS controller.Emmmanuel FIADUFERank #947 · 2.2K reputationPosted Jun 30, 2023, 5:30 AM EDT
Thank you Vishal and Deepak for swift response, it working now