<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的值为空字符串(“”)。
答案 0 :(得分:32)
试试这样:
<%= 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('Are you sure you want to delete this User?');">Delete</a>
另请注意,建议不要使用普通的GET动词来修改服务器上的状态。
以下是我推荐你的内容:
[HttpDelete]
public ActionResult DeleteUser(string uname)
{
//delete user
}
并在视图中:
<% 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。