Moti Mashiah
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,
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,
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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:
Here is my code by your suggestion:
Thank you sooo much
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.");
}
});
}
}
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);
}
Thank you sooo much
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:
controller:
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.");
}
});
}
}
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");
}
ASKER
Thanks, and look at my last comment solution.
ASKER
Html:
Open in new window
Open in new window
Now I need to know how to return from the controller.
please help.