Avatar of Moti Mashiah
Moti Mashiah
Flag for Canada asked on

asp.net mvc

Hi Guys,

I added two entity tables in my asp.net mvc application between these tables there is FK

table-order and table customer:

the relationship between the tables - CustomerID = id

What I'm trying to do is - when user want to delete customer that belong to some order he will get some alert which says "you can't delete user belong to some order....".

can somebody explain how to do it with Ajax...like to send from view to the controller and check if customerid = id so if true don't delete if false delete.

Please, if you can send some example code I will be very grateful.


Thanks,
.NET ProgrammingC#ASP.NET

Avatar of undefined
Last Comment
Moti Mashiah

8/22/2022 - Mon
Moti Mashiah

ASKER
I just did something like this in the UI:

Html:
<a class="icon-remove icon-2x" onclick="Deleterep('@item.Id')" title="Delete" href="/Customer/Delete/"></a>

Open in new window


        function Deleterep(id) {
            var res = confirm('You want to delete this PO??');
            if (res) {
                $.ajax({
                    type: "POST",
                    url: "/Purchase/Deletere",
                    dataType: "html",
                    data: { cusid: id },
                    success: function (data) {
                        $("#table-content").html(data);
                    }, error: function (xhr, err) {
                        alert("Error occured, Please try again.");
                    }
                });
            }
        }

Open in new window


Now I need to know how to return from the controller.

please help.
ASKER CERTIFIED SOLUTION
Carl Tawn

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Moti Mashiah

ASKER
Hi Carl,

As usual you are the best answer in this site....it is working now but i have one more issue.

1. when I return the answer "can't delete" I don't get the view back I get just the answer. how can I return the answer and  the view together? I was trying something like this - return RedirectToAction("Index", Message); what didn't work.

2. I would like to return message to ajax and popup message to user like alert message:
here is my ajax code:
function Deleterep(cusid) {
            var res = confirm('You want to delete this Customer??');
            if (res) {
                $.ajax({
                    type: "POST",
                    url: "/Customer/Deletere",
                    dataType: "html",
                    data: { cusid: cusid },
                    success: function (data) {
                        $("#table-content").html(data);
                    }, error: function (xhr, err) {
                        alert("Error occured, Please try again.");
                    }
                });
            }
        }

Open in new window


Here is my code by your suggestion:

 public ContentResult Deletere(int cusid)
        {
            string message = "Cannot delete";   // replace with your own message

            BayDiamondEntities db = new BayDiamondEntities();
            int count = db.t_Repair.Where(o => o.CustomerID == cusid).Count();
            if (count == 0)
            {
                t_Customer customer = db.t_Customer.Where(c => c.Id == cusid).SingleOrDefault();
                if (customer != null)
                {
                    db.t_Customer.Remove(customer);
                    db.SaveChanges();
                    message = "Done";
                }
            }

            return Content(message);
        }

Open in new window



Thank you sooo much
Moti Mashiah

ASKER
Hi Carl ,

I ended up with something like that (see bellow code) and it is working great.
Also i did a constraint on the database.

AJAX:
function Deleterep(cusid) {
            var res = confirm('You want to delete this Customer??');
            if (res) {
                $.ajax({
                    type: "POST",
                    url: "/Customer/Deletere",
                    dataType: "html",
                    data: { cusid: cusid },
                    success: function (data) {
                        if (data == "1") {
                            alert("Deleted");
                            window.location = "/Customer/index";
                        }
                        alert("You can not delete");
                    }, error: function (xhr, err) {
                        alert("Error occured, Please try again.");
                    }
                });
            }
        }

Open in new window


controller:

 public ActionResult Deletere(int cusid)
        {
            BayDiamondEntities db = new BayDiamondEntities();
            int count = db.t_Repair.Where(o => o.CustomerID == cusid).Count();
            if (count == 0)
            {
                t_Customer customer = db.t_Customer.Where(c => c.Id == cusid).SingleOrDefault();
                if (customer != null)
                {
                    db.t_Customer.Remove(customer);
                    db.SaveChanges();
                }
                return Content("1");
            }
            else
                return Content("0");
        }

Open in new window

Your help has saved me hundreds of hours of internet surfing.
fblack61
Moti Mashiah

ASKER
Thanks, and look at my last comment  solution.