如何在进入控制器操作之前使用操作链接来获得确认弹出窗口
How to use an action link to get a confirmation popup before going to a controller action?
我有一个操作链接,它调用一个控制器方法并传递一些数据来启用或禁用数据库系统中的用户。
@Html.ActionLink("Change Status", "ChangeStatus", "Home", new { userName = ViewBag.userName, applicationName = item.Key, currentStatus = item.Value }, new { @class = "enableDisable" })
这个链接工作正常,除了在调用控制器方法之前应该会出现一条JQuery弹出消息来确认操作。但是,操作链接绕过弹出窗口,直接调用控制器。
@section Scripts {
<link rel="stylesheet" href="http://code.jquery.com/ui/1.11.1/themes/smoothness/jquery-ui.css">
<script src="http://code.jquery.com/ui/1.11.1/jquery-ui.js"></script>
<script type="text/javascript">
$("#dialog-confirm").hide();
$(function () {
$(".enableDisable").click(function () {
$("#dialog-confirm").dialog({
resizable: false,
height: 220,
width: 475,
modal: true,
buttons: {
"OK": function () {
$(this).dialog("close");
window.location.href = "~/Home/ChangeStatus/userName/applicationName/currentStatus/"
},
Cancel: function () {
$(this).dialog("close");
}
}
});
});
});
</script>
}
有人知道我的代码中缺少什么吗?
编辑
JQuery现在位于一个js文件中,由以下代码调用:
@section Scripts {
<link href="~/Content/jquery-ui-1.11.1.css" rel="stylesheet" />
<script src="~/Scripts/jquery-ui-1.11.1.js"></script>
<script src="~/Scripts/Dialog.js"></script>
}
JQuery本身现在看起来是这样的:
$("#dialog-confirm").hide();
$(function () {
$(".enableDisable").click(function (e) {
e.preventDefault()
var url = $(this).attr('href');
$("#dialog-confirm").dialog({
resizable: false,
height: 220,
width: 475,
modal: true,
buttons: {
"OK": function () {
$(this).dialog("close");
window.location.href = url;
},
Cancel: function () {
$(this).dialog("close");
}
}
});
});
});
Razor视图:
@{
ViewBag.Title = "User Details";
}
<h2>User Details</h2>
<p><b>@ViewBag.UserName</b></p>
<table class="table">
<tr>
<th>
Application Name
</th>
<th>
Status
</th>
<th>
</th>
<th>
</th>
</tr>
@if (ViewBag.ApplicationStatuses.Count > 0)
{
@*Iterating Amadeus model using ViewBag *@
foreach (var item in ViewBag.ApplicationStatuses)
{
<tr>
<td>
@item.Key
</td>
<td>
@item.Value
</td>
<td>
@Html.ActionLink("Change Status", "ChangeStatus", "User", new { userName = ViewBag.userName, applicationName = item.Key }, new { @class = "enableDisable" })
</td>
<td>
@Html.ActionLink("View Roles", "Roles", new { userName = ViewBag.UserName, applicationName = item.Key })
</td>
</tr>
}
}
</table>
<div id="dialog-confirm" title="Change Status?">
Are you sure you want to change the status of: @ViewBag.UserName?
</div>
@section Scripts {
<link href="~/Content/jquery-ui-1.11.1.css" rel="stylesheet" />
<script src="~/Scripts/jquery-ui-1.11.1.js"></script>
<script src="~/Scripts/Dialog.js"></script>
}
尝试使用e.preventDefault()
并进行一些更改,如下所示:-
<script type="text/javascript">
//$("#dialog-confirm").hide();
$(function () {
$(".enableDisable").click(function (e) {
e.preventDefault();
var url = $(this).attr('href');
$("#dialog-confirm").dialog({
resizable: false,
height: 220,
width: 475,
modal: true,
buttons: {
"OK": function () {
//$(this).dialog("close");
window.location.href = url; //<-------
},
"Cancel": function () {
$(this).dialog("close");
}
}
});
$("#dialog-confirm").dialog("open"); // <------- open dialog this way.
});
});
</script>
相关文章:
- Javascript Show Hide-将切换操作从复选框更改为<a href>链接
- 在Redux中链接async和sync操作以计算初始状态
- 更改跳转到URL操作SSRS 2008 R2的链接目标属性(rc:LinkTarget)
- reactJs redux:如何对用户事件调度操作并将操作与化简器和存储链接
- 如何从 MVC4 链接调用 Javascript 方法(错误 JavaScript 运行时错误:对象不支持此操作)
- 我应该只在指令的链接 fn 中进行 DOM 操作吗?
- 如何使用 MVC3 Razor 在 _layout.cshmtl 上创建悬停下拉操作链接
- 无法将操作链接绑定到 html 按钮
- ASP.Net JavaScript 表的 MVC 操作链接/URL 操作 - datatable.net
- 如何从用户输入的文本框中从 Html 操作链接控件捕获传递多个值并发送到操作方法
- 跨度内操作链接
- PDF中的自定义操作链接.编写Javascript以更改单击时链接的外观
- 在ASP.NET MVC3剃刀上使用javascript变量动态创建html操作链接
- 如何在进入控制器操作之前使用操作链接来获得确认弹出窗口
- Javascript -如何“请求查询字符串”从Url.检索参数值的操作链接
- 当呈现的操作链接作为查询字符串返回时,我如何修复MVC3的路由问题
- asp.net mvc-javascript或ajax操作链接
- j查询单击操作链接不执行任何操作
- 如何在运行时使用 MVC 5 中的 JavaScript ASP.NET 使用路由值扩充操作链接
- 防止 javascript 函数生成删除操作链接(如果登录的用户角色为关联)