<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" /> <% } %>
$(function() { $('#myform').submit(function() { return confirm('Are you sure you want to delete this User?'); }); });
您也可以考虑添加一个anti forgery token来保护此操作免于CSRF attacks.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。