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

<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
}

控制器中uname的值正在传递为空字符串(“”).

解决方法

尝试这样:
<%= Html.ActionLink(
    "Delete","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
}

并认为:

<% using (Html.BeginForm(
    "DeleteUser",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.

相关文章

项目要求通过网站上传大文件,比如视频文件,通过摸索实现了文件分片来上传,然后后台进行合并。 使用了...
安装新版本的Nginx(vim /etc/yum.repos.d/nginx.repo) [nginx-stable] name=nginx stable repo baseu...
什么是 SignalR&#160;ASP.NET Core ASP.NET Core SignalR 是一种开放源代码库,可简化将实时 web 功...
在Windows下使用Docker,我们选择Docker Desktop这个软件,非常方便。 ## Docker Desktop介绍及安装 Do...
项目开始设计的是运行在windows下,所以一开始采用的是windows服务模式来获取多媒体文件信息,后来要求...
银河麒麟高级服务器操作系统V10是针对企业级关键业务,适应虚拟化、云计算、大数据、工业互联网时代对主...