处理用户重新提交删除操作的正确方法?

发布于 2024-10-15 02:44:22 字数 982 浏览 3 评论 0原文

假设用户删除一条记录,然后按后退箭头,并重新提交 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

给妤﹃绝世温柔 2024-10-22 02:44:22

处理此问题的 RESTFul 方法是抛出 404 Not Found (因为用户尝试删除不再存在的记录):

if (ret.Count() == 0)
{
    throw new HttpException(404, "Not found");
}

另一种方法是将错误添加到模型状态并重新显示视图:

if (ret.Count() == 0)
{
    ModelState.AddModelError("id", "An item with the specified id was not found");
    return View();
}

在视图内您将有验证摘要或 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):

if (ret.Count() == 0)
{
    throw new HttpException(404, "Not found");
}

Another way is to add an error into the model state and redisplay the view:

if (ret.Count() == 0)
{
    ModelState.AddModelError("id", "An item with the specified id was not found");
    return 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 :-)

柠栀 2024-10-22 02:44:22

在这种情况下,您需要使用 TempData。

 if (ret.Count() == 0)
 {
   TempData["ErrorMessage"] = "Record <Number> does not exist";
   return RedirectToAction("Index");

}

然后,您可以访问视图中的 TempData 以显示消息。请参阅此链接 了解更多详情

You'd want to make use of TempData in this case.

 if (ret.Count() == 0)
 {
   TempData["ErrorMessage"] = "Record <Number> does not exist";
   return RedirectToAction("Index");

}

You can then access the TempData in the view to display the message. Refer this link for more details

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文