我有一个删除记录的表单。一个表单我正在使用删除按钮,当单击按钮时,表单将被提交。
<input type="submit" value="Delete" />
如果删除或取消已被点击,我如何实施取消并检查操作?
答案 0 :(得分:7)
您必须为输入name
。例如:
<input type='submit' value='Delete' name='action' />
<input type='submit' value='Cancel' name='action' />
然后在你的行动中:
[HttpPost]
public ActionResult Submit(string action) {
if (action == "Delete") {
// User clicked "Delete"
} else {
// User clicked "Cancel"
}
}
答案 1 :(得分:2)
您真的需要在控制器中处理取消点击事件吗?您可能只是在取消时将用户重定向到其他页面,对吧? Javascript对我来说非常好用:
<input type="button" value="Cancel"
onclick="window.location='<%: Url.Action("Details", new { id = Model.Id }) %>'" />
答案 2 :(得分:0)
使用jQuery(好吧,你也可以只在JavaScript中完成),你可以做类似的事情:
$('input[type="submit"]').click(function(e) {
if(confirm('Are you sure?') {
// do delete
}
});
答案 3 :(得分:0)
我的Intro to ASP.NET MVC 3解释了这一点。确保不要在GET上删除。
答案 4 :(得分:-1)
关于Stephan Walter's site ......
MVC应用
如何在[ChildActionOnly]
中使用ActionResult
属性并使用某些身份验证来防止意外删除?如果你使用带有ActionResult
的jQuery GET回调来呈现PartialView
,他建议REST纯粹主义者会捍卫GET请求不应该改变你的应用程序状态的想法怎么办?喜欢:
//delete comment and make ajax callback
function del(aid, cid) {
$.ajax("/Article/DelCom?AId=" + aid + "&" + "CId=" + cid, function (result) {
$('#comments-partial').html(result);
});
或者这仅适用于链接?