C# 在进入控制器操作之前,如何使用操作链接获得确认弹出窗口?
我有一个动作链接,它调用控制器方法并传递一些数据以启用或禁用数据库系统中的用户C# 在进入控制器操作之前,如何使用操作链接获得确认弹出窗口?,c#,javascript,jquery,asp.net-mvc-5,actionlink,C#,Javascript,Jquery,Asp.net Mvc 5,Actionlink,我有一个动作链接,它调用控制器方法并传递一些数据以启用或禁用数据库系统中的用户 @Html.ActionLink("Change Status", "ChangeStatus", "Home", new { userName = ViewBag.userName, applicationName = item.Key, currentStatus = item.Value }, new { @class = "enableDisable" }) 除了在调用控制器方法之前出现JQuery弹出消息
@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>
}
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>
}
@{
ViewBag.Title=“用户详细信息”;
}
用户详细信息
@ViewBag.UserName
应用程序名称
地位
@如果(ViewBag.ApplicationStatus.Count>0)
{
@*使用ViewBag迭代Amadeus模型*@
foreach(ViewBag.ApplicationStatus中的变量项)
{
@项目.关键
@项目.价值
@ActionLink(“更改状态”、“更改状态”、“用户”、新{userName=ViewBag.userName、applicationName=item.Key}、新{@class=“enabledesable”})
@ActionLink(“查看角色”、“角色”,新{userName=ViewBag.userName,applicationName=item.Key})
}
}
是否确实要更改:@ViewBag.UserName的状态?
@节脚本{
}
尝试使用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>
//$(“#对话框确认”).hide();
$(函数(){
$(“.enableDisable”)。单击(函数(e){
e、 预防默认值();
var url=$(this.attr('href');
$(“#对话框确认”)。对话框({
可调整大小:false,
身高:220,
宽度:475,
莫代尔:是的,
按钮:{
“OK”:函数(){
//$(此).dialog(“关闭”);
window.location.href=url;//不幸的是,这没有帮助,我已经更新了我的问题,以提供js文件中的代码和调用js文件的代码。@SlipperyBalmain…抱歉,但我没有得到您现在想要做的事情…在我的回答中,您没有在…函数(e)中使用“e”…???嗯,实际上这是一个很好的观点,我现在有代码,jquery不起作用。我现在似乎在做一些奇怪的更改。我正在尝试做同样的事情,但我的jquery不是和动作链接在同一个文件中,而是在一个js文件中,并且包含动作链接的文件中有一个脚本引用。我有现在包含了整个razor代码供查看。我找不到。问题似乎是,当我尝试更改任何应用程序的状态时,只有列表中的第一个应用程序被更新。只有当我的弹出窗口被激活并且javascript位于razor视图的单独文件中时,问题才会出现。通过调试,我发现jquery弹出按钮仅查找创建的第一个操作链接的href,但我似乎无法理解它在同一文件中工作的原因。这是否更好地解释了问题?我可以采取哪些步骤来解决此问题?
@{
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>
}
<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>