C#,MVC确认删除对话框
在乞讨会上,我想为我糟糕的英语技能道歉。我使用带有实体框架的MVC创建web应用程序。当用户试图从表中删除记录时,我想创建一个自定义的确认对话框。当我使用标准的确认对话框“return confirm('你确定吗?')”时,确认就起作用了。但是我想使用一个定制的确认对话框,比如来自bootstrap的sweet alert或类似的东西 控制器中的删除方法:C#,MVC确认删除对话框,c#,jquery,asp.net-mvc,twitter-bootstrap,sweetalert,C#,Jquery,Asp.net Mvc,Twitter Bootstrap,Sweetalert,在乞讨会上,我想为我糟糕的英语技能道歉。我使用带有实体框架的MVC创建web应用程序。当用户试图从表中删除记录时,我想创建一个自定义的确认对话框。当我使用标准的确认对话框“return confirm('你确定吗?')”时,确认就起作用了。但是我想使用一个定制的确认对话框,比如来自bootstrap的sweet alert或类似的东西 控制器中的删除方法: [HttpPost] [ValidateAntiForgeryToken] [Authorize]
[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>
}
@foreach(模型中的变量项)
{
@DisplayFor(modelItem=>item.Result)
@DisplayFor(modelItem=>item.Time)
@使用(Html.BeginForm(“Delete”,“DiabeticControls”,new{id=item.id}))
{
@Html.AntiForgeryToken()
@ActionLink(“编辑”,“编辑”,新的{id=item.id},新的{@class=“btn btn default xs margin”})
@ActionLink(“Details”,“Details”,new{id=item.id},new{@class=“btn btn default xs margin”})
删除
}
@{
ViewBag.Key=item.Id;
}
@Html.HiddenFor(modeleItem=>item.UserId)
}
我在中发现了一个简单的问题:但它在我的项目中不起作用
还有我的JavaScript:
@节脚本{
<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>
<script type="text/javascript">
$(document).ready(function () {
$('.delete').on('click', function (e, data) {
if (!data) {
handleDelete(e, 1);
} else {
window.location = $(this).attr('href');
}
});
});
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) {
$(e.target).trigger('click', {});
}
});
}
};
</script>
$(函数(){
$('.delete')。在('click',函数(e,数据){
如果(!数据){
handledelite(e,1);
}否则{
window.location=$(this.attr(@Url.Action(“DiabeticControls”)));
}
});
});
功能手柄(e,停止){
如果(停止){
e、 预防默认值();
游泳({
标题:“你确定吗?”,
文本:“您将无法再次恢复delaer!”,
键入:“警告”,
showCancelButton:true,
confirmButtonColor:#DD6B55“,
confirmButtonText:“是,删除!”,
closeOnConfirm:false
},
功能(isConfirm){
如果(我确认){
swal(“删除”、“成功”);
$('.delete').trigger('click',{});
}
否则{
swal(“取消”、“错误”);
}
});
}
};
}好的,非常感谢您的帮助,但我找到了解决方案。问题出在我所附的图书馆里。我用不同版本的jquery.js、sweetalert.css和sweetalert.js替换它,并根据本文中的解决方案定制了我的JavaScript: 我的JavaScript: @节脚本{
<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>
<script type="text/javascript">
$(document).ready(function () {
$('.delete').on('click', function (e, data) {
if (!data) {
handleDelete(e, 1);
} else {
window.location = $(this).attr('href');
}
});
});
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) {
$(e.target).trigger('click', {});
}
});
}
};
</script>
$(文档).ready(函数(){
$('.delete')。在('click',函数(e,数据){
如果(!数据){
handledelite(e,1);
}否则{
window.location=$(this.attr('href');
}
});
});
功能手柄(e,停止){
如果(停止){
e、 预防默认值();
游泳({
标题:“你确定吗?”,
文本:“您将无法再次恢复delaer!”,
键入:“警告”,
showCancelButton:true,
confirmButtonColor:#DD6B55“,
confirmButtonText:“是,删除!”,
closeOnConfirm:false
},
功能(isConfirm){
如果(我确认){
$(e.target).trigger('click',{});
}
});
}
};
}请发布您的JavaScriptOk,我在主帖子中添加了JavaScript;)我看到了对话框,但即使我单击“是”,也不会发生任何事情。
closeOnConfirm:false
可能与此有关。不,我试图删除这行代码或将其设置为true,但不幸的是,它不起作用。但是我当然非常感谢你帮我;)