What's the best way to perform a confirmation pop out when click the actionlink to perform a delete? Currently when i press the delete action link, it will straight delete. Anyway to perform a pop out confirmation box to delete? Thanks!
My View:
<td>
@Html.ActionLink("Delete", "SURV_Question_Delete", "SURV_Question", new { Question_ID = Model[i].Question_ID }, null)
</td>
My Controller:
public ActionResult SURV_Question_Delete(int Question_ID)
{
var query = from r in db.SURV_Question_Ext_Model.ToList()
where r.Qext_Question_ID == Question_ID
select r;
foreach(var item in query)
{
db.SURV_Question_Ext_Model.Remove(item);
db.SaveChanges();
}
return RedirectToAction("SURV_Main_Edit", "SURV_Main", new { id = surveyId });
}
What's the best way to perform a confirmation pop out when click the actionlink to perform a delete? Currently when i press the delete action link, it will straight delete. Anyway to perform a pop out confirmation box to delete? Thanks!
My View:
<td>
@Html.ActionLink("Delete", "SURV_Question_Delete", "SURV_Question", new { Question_ID = Model[i].Question_ID }, null)
</td>
My Controller:
public ActionResult SURV_Question_Delete(int Question_ID)
{
var query = from r in db.SURV_Question_Ext_Model.ToList()
where r.Qext_Question_ID == Question_ID
select r;
foreach(var item in query)
{
db.SURV_Question_Ext_Model.Remove(item);
db.SaveChanges();
}
return RedirectToAction("SURV_Main_Edit", "SURV_Main", new { id = surveyId });
}
Share
Improve this question
asked Jul 27, 2015 at 6:08
Edward.KEdward.K
5663 gold badges13 silver badges33 bronze badges
3
-
1
Firstly, a delete action should not be a GET, it should be a POST. Use javascript/jquery to handle the
.click()
event (or more correctly the.submit()
event of the form) and display an confirm/popup and based on the return value, cancel the submit – user3559349 Commented Jul 27, 2015 at 6:11 - @StephenMuecke any code sample for me to easy refer? Thanks. – Edward.K Commented Jul 27, 2015 at 6:16
- check anser might help you in what you are looking for – Pranay Rana Commented Jul 27, 2015 at 6:22
3 Answers
Reset to default 3Firstly, your delete action should be a POST, not a GET. Your modifying data and do not want this added to the browser history, or allow the url to be entered in the address bar (the item may have already been deleted)
Change your html to
@using (Html.BeginForm("SURV_Question_Delete", "SURV_Question", new { Question_ID = Model[i].Question_ID }))
{
@Html.AntiForgeryToken()
<input type="submit" value="delete" />
}
and decorate the method with the [HttpPost]
and [ValidateAntiForgeryToken]
attributes.
Then add a script
$('form').submit(function() {
// Modify the following to use you jquery-ui-dialog, but for testing purposes
if (!confirm("Are you sure want to delete record") {
return false; // cancel the submit if the user clicked the Cancel button
}
});
Try like this, add online on link which call javascript function for confirmation.
@Html.ActionLink("Delete", "SURV_Question_Delete", "SURV_Question",
new { Question_ID = Model[i].Question_ID, onclick = "return
DeleteConfirm()" }, null)
function DeleteConfirm()
{
if (confirm("Are you sure want to delete record"))
return true;
else
return false;
}
you can also try this
@Html.ActionLink("Delete", "SURV_Question_Delete", "SURV_Question",
new { Question_ID = Model[i].Question_ID, onclick="return confirm('Are you sure?');"},null);
You also can do the following, which will show the confirm box with the value you want to delete, which is more perfect in case of deleting.
@Html.ActionLink("Delete Subject", "DeleteSubject", "Subjects", new { SubjectId = _item.SubjectMsId },
new { onClick = "return confirm('Are you sure, you want to Delete the Subject : "+
@_item.SubjectName+" ?')" })