我正在开发一个MVC网站,对于我的删除功能,我决定使用jQuery UI Dialog显示一个弹出式对话框,供用户确认他们希望删除该对象。我的问题是它没有按预期显示,当我选择删除时,我可以看到我的部分视图对话框弹出一秒钟,然后重定向到显示我的确认消息和要删除的按钮的另一个页面。
这是我的删除控制器:
//Deletes a selected club
[HttpGet]
public ActionResult DeletePartialView(int? id) //Original: public ActionResult Delete(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Club club = db.Clubs.Find(id);
if (club == null)
{
return HttpNotFound();
}
return PartialView(club);
}
[HttpPost, ActionName("DeletePartialView")]
public ActionResult DeleteConfirmed(int id) //Original: public ActionResult DeleteConfirmed(int id)
{
Club club = db.Clubs.Find(id);
var MembersToDelete = club.ClubMembers.ToList();
foreach (var item in MembersToDelete)
{
db.ClubMembers.Remove(item);
}
db.Clubs.Remove(club);
db.SaveChanges();
return RedirectToAction("Index");
}
这是删除按钮和div中的局部视图:
@Html.ActionLink("Delete", "Delete", new { id = item.ClubID }, new { @class = "btn btn-danger btn-xs" }) |
@*@Html.ActionLink("Delete Partial", "DeletePartialView", new { id = item.ClubID }, new { @class = "btn btn-danger btn-xs" })*@
@Html.ActionLink(
"Delete Partial",
"DeletePartialView",
new { id = item.ClubID },
new
{
@class = "btn btn-danger btn-xs",
id = "deleteClub-opener" //Button ID
})
@* Delete Club Popup*@
<div id="DelteClub-dialog" title="Delete Club">
@Html.Partial("DeletePartialView", new ultimateorganiser.Models.Club())
</div>
这是jQuery代码:
//Delete Club Dialog Window with effects
$(function () {
$("#DelteClub-dialog").dialog({
autoOpen: false,
height: 500,
width: 600,
show: {
effect: "clip",
duration: 500
},
hide: {
effect: "highlight",
duration: 1000
}
});
//Open Delete Club Dialog Window
$("#deleteClub-opener").click(function () {
$("#DelteClub-dialog").dialog("open");
});
})
这就是我的DeletePartialView:
@model ultimateorganiser.Models.Club
@{
ViewBag.Title = "Delete";
}
<h3 class="text-warning">Are you sure you want to delete this club?</h3>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-actions no-color">
<input type="submit" value="Delete" class="btn btn-danger" />
@Html.ActionLink("Back to List", "Index")
</div>
}
答案 0 :(得分:0)
您可以在preventDefault
绑定中使用jQuery
。
$("#deleteClub-opener").click(function (e) {
e.preventDefault();
$("#DelteClub-dialog").dialog("open");
});
或者,您也可以在绑定函数中return false
来阻止事件传播。
答案 1 :(得分:0)
到目前为止,你现在好了。要进行删除工作,请在开始表单
后的删除部分视图中添加以下内容<input type="hidden" name="id" value="@Model.Id"/>
答案 2 :(得分:0)
please check this code.and tell me another problem for using the dialog box.
only use this library
<html>
<head>
<link href="~/library/jquery-ui.min.css" rel="stylesheet" />
<script src="~/library/jquery.js"></script>
<script src="~/library/jquery-ui.min.js"></script>
</head>
<div>
<button id="btnCreate" class="btn-btn-primary">open the dialog</button>
</div>
<script type="text/javascript">
$(document).ready(function () {
$(function () {
$.noConflict(true);
$("#dialog").dialog({
autoOpen: false,
draggable: true,
resizable: true,
dialogClass: "alert",
modal: true
});
$("#btnCreate").click(function () {
$('#dialog').dialog('open');
});
});
});
<body>
<div id="dialog" style ="display:none" class="form" title="Basic dialog">
<table>
<tr>
<th>Name</th>
</tr>
<tr>
<th>Name</th>
<td><input type ="text" id="txtName" style= "width:200px" />
</tr>
<tr>
<th>Age</th>
<td><input type ="text" id="txtAge" style= "width:200px" />
</tr>
<tr>
<td><input type="submit" value="Create" id="btnCreateId" class="btn btn-Sucess" />
<td><input type="button" value="Cancel" id="txtCancel" class="btn btn-danger"/>
</tr>
</table>
</div>
</body>
<html>