Link to home
Start Free TrialLog in
Avatar of Moti Mashiah
Moti MashiahFlag 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,
Avatar of Moti Mashiah
Moti Mashiah
Flag of Canada image

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
Avatar of Carl Tawn
Carl Tawn
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
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

Thanks, and look at my last comment  solution.