ASP.NET MVC删除操作链接确认
发布时间:2020-12-30 11:50:55 所属栏目:asp.Net 来源:互联网
导读: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
<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('Are you sure you want to delete this User?');">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. (编辑:岳阳站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
推荐文章
站长推荐
- asp.net – 如何禁用.NET事件日志警告?
- asp.net-mvc – ASP .Net MVC 3:子动作和重定向
- asp.net – 转发器控件中的单选按钮列表
- asp.net core标签助手的高级用法TagHelper+Form
- asp.net – 双回发问题
- asp.net-mvc – 如何通过URL传递日期,为我的Acti
- asp.net-mvc – 是否可以在基于路由的MVC4中使用
- asp.net-mvc – 为什么ASP.NET MVC使用会话状态?
- asp.net-mvc – Asp .Net Core – 无法安装Micro
- asp.net-mvc – 在Azure Active Directory B2C中
热点阅读