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,
LVL 1
Moti Mashiah.NET DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Moti Mashiah.NET DeveloperAuthor Commented:
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.
0
Carl TawnSystems and Integration DeveloperCommented:
Normally you would have a constraint on the database to prevent that kind of deletion from taking place.

So your two main options are:

1) Check for related orders first and return an error if there are any (the clean way)
2) Attempt the delete and catch the exception (dirty way)

Sample for option 1:
        [AcceptVerbs(HttpVerbs.Post)]
        public ContentResult Delete( int cusid )
        {
            string message = "Cannot delete";   // replace with your own message

            WibbleEntities db = new WibbleEntities();
            int count = db.Orders.Where(o => o.CustomerID == cusid).Count();
            if (count == 0) {
                Customer customer = db.Customers.Where(c => c.ID == cusid).SingleOrDefault();
                if (customer != null)
                {
                    db.Customers.Remove(customer);
                    db.SaveChanges();
                    message = "Done";
                }
            }

            return Content(message);
        }

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Moti Mashiah.NET DeveloperAuthor Commented:
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
0
Moti Mashiah.NET DeveloperAuthor Commented:
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

0
Moti Mashiah.NET DeveloperAuthor Commented:
Thanks, and look at my last comment  solution.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.