【发布时间】:2017-12-20 12:30:57
【问题描述】:
在开始时,我想为我糟糕的英语技能感到抱歉。我使用带有实体框架的 MVC 创建 Web 应用程序。当用户尝试从表中删除记录时,我想创建一个自定义的确认对话框。当我使用标准确认对话框“返回确认('你确定吗?')”时,确认有效。但是我想使用自定义的确认对话框,例如 bootstrap 中的甜蜜警报或类似的东西。
Controller 中的删除方法:
[HttpPost]
[ValidateAntiForgeryToken]
[Authorize]
public ActionResult Delete(int id)
{
DiabeticControl diabeticControl = db.DiabeticControls.Find(id);
db.DiabeticControls.Remove(diabeticControl);
db.SaveChanges();
return RedirectToAction("Index");
}
视图中的表:
@foreach (var item in Model)
{
<tr>
<td>@Html.DisplayFor(modelItem => item.Result)</td>
<td>@Html.DisplayFor(modelItem => item.Time)</td>
<td>
<div class="form-group">
@using (Html.BeginForm("Delete", "DiabeticControls", new { id = item.Id }))
{
@Html.AntiForgeryToken()
@Html.ActionLink("Edit", "Edit", new { id = item.Id }, new { @class = "btn btn-default xs-margin" })
@Html.ActionLink("Details", "Details", new { id = item.Id }, new { @class = "btn btn-default xs-margin" })
<button type="submit" class="btn btn-danger xs-margin delete" >Delete</button>
}
</div>
@{
ViewBag.Key = item.Id;
}
</td>
<td>@Html.HiddenFor(modelItem => item.UserId)</td>
</tr>
}
我在enter link description here 中找到了一个简单的问题,但它在我的项目中不起作用。
还有我的 JavaScript:
@section 脚本{
<script type="text/javascript">
$(function () {
$('.delete').on('click', function (e, data) {
if (!data) {
handleDelete(e, 1);
} else {
window.location = $(this).attr("@Url.Action("DiabeticControls")");
}
});
});
function handleDelete(e, stop) {
if (stop) {
e.preventDefault();
swal({
title: "Are you sure?",
text: "You will not be able to recover the delaer again!",
type: "warning",
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: "Yes, delete!",
closeOnConfirm: false
},
function (isConfirm) {
if (isConfirm) {
swal("Deleted", "", "success");
$('.delete').trigger('click', {});
}
else {
swal("Cancelled", "", "error");
}
});
}
};
</script>
}
【问题讨论】:
-
请发布您的 JavaScript
-
好的,我在主帖中添加了 JavaScript ;)
-
我看到了对话框,但即使我点击是,也不会发生任何事情。
-
closeOnConfirm: false可能与此有关。 -
不,我试图删除这行代码或将其设置为 true,但不幸的是它不起作用。但是,当然非常感谢您尝试帮助我;)
标签: c# jquery asp.net-mvc twitter-bootstrap sweetalert