处理用户重新提交删除操作的正确方法?
假设用户删除一条记录,然后按后退箭头,并重新提交 POST 请求。
处理这种情况我有什么选择?
什么是首选?
[HttpPost]
public ActionResult Delete(string EntryName, Guid id, FormCollection collection)
{
try
{
var ret = from m in _entities.MyList
where m.MyListID == id
&& m.EntryName == EntryName
select m ;
if (ret.Count() == 0)
{
// This happens if the user pressed the back button and resubmitted
// todo: ask SO what is the best way to approach this...
// User feedback? How?
return RedirectToAction("Index", new { id = id });
}
_entities.DeleteObject(ret.FirstOrDefault());
_entities.SaveChanges();
return RedirectToAction("Index", new { id = id } );
}
catch
{
return View();
}
}
Suppose a user deletes a record, and then presses the back arrow, and resubmits the POST request.
What are my options in handling this scenario?
What is preferred?
[HttpPost]
public ActionResult Delete(string EntryName, Guid id, FormCollection collection)
{
try
{
var ret = from m in _entities.MyList
where m.MyListID == id
&& m.EntryName == EntryName
select m ;
if (ret.Count() == 0)
{
// This happens if the user pressed the back button and resubmitted
// todo: ask SO what is the best way to approach this...
// User feedback? How?
return RedirectToAction("Index", new { id = id });
}
_entities.DeleteObject(ret.FirstOrDefault());
_entities.SaveChanges();
return RedirectToAction("Index", new { id = id } );
}
catch
{
return View();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
处理此问题的 RESTFul 方法是抛出 404 Not Found (因为用户尝试删除不再存在的记录):
另一种方法是将错误添加到模型状态并重新显示视图:
在视图内您将有验证摘要或
id
的验证消息以显示消息。PS:那里的 TODO 评论很好:-)
A RESTFul way to handle this is to throw a 404 Not Found (because the user tried to delete a record that no longer exists):
Another way is to add an error into the model state and redisplay the view:
and inside the view you would have a validation summary or a validation message for
id
to display the message.P.S.: Nice TODO comment over there :-)
在这种情况下,您需要使用 TempData。
然后,您可以访问视图中的 TempData 以显示消息。请参阅此链接 了解更多详情
You'd want to make use of TempData in this case.
You can then access the TempData in the view to display the message. Refer this link for more details