gpt4 book ai didi

ASP.NET MVC 删除操作链接并确认

转载 作者:行者123 更新时间:2023-12-04 02:38:13 26 4
gpt4 key购买 nike

      <td>
<%= Html.ActionLink("Delete", "DeleteUser", new RouteValueDictionary(new {uname=item.UserName}), new { onclick = "return confirm('Are you sure you want to delete this User?');" }) %>
</td>

在 Global.asax.cs
routes.MapRoute(
"DeleteUser",
"Account.aspx/DeleteUser/{uname}",
new { controller = "Account", action = "DeleteUser", uname = "" }
);

在 ActionContorller.cs 中
public ActionResult DeleteUser(string uname)
{
//delete user
}

正在传递的 Controller 中的 uname 值是空字符串(“”)。

最佳答案

像这样尝试:

<%= Html.ActionLink(
"Delete",
"DeleteUser",
"Account",
new {
uname = item.UserName
},
new {
onclick = "return confirm('Are you sure you want to delete this User?');"
}
) %>

然后确保生成的链接正确:
<a href="/Account.aspx/DeleteUser/foo" onclick="return confirm(&#39;Are you sure you want to delete this User?&#39;);">Delete</a>

另请注意,不建议对修改服务器状态的操作使用简单的 GET 动词。

以下是我向您推荐的:
[HttpDelete]
public ActionResult DeleteUser(string uname)
{
//delete user
}

并在 View 中:
<% using (Html.BeginForm(
"DeleteUser",
"Account",
new { uname = item.UserName },
FormMethod.Post,
new { id = "myform" })
) { %>
<%= Html.HttpMethodOverride(HttpVerbs.Delete) %>
<input type="submit" value="Delete" />
<% } %>

并在一个单独的 javascript 文件中:
$(function() {
$('#myform').submit(function() {
return confirm('Are you sure you want to delete this User?');
});
});

您也可以考虑添加 anti forgery token保护此操作免受 CSRF attacks .

关于ASP.NET MVC 删除操作链接并确认,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5463680/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com