我有我的asp.net MVC应用程序内的以下观点,即包含了删除对象的ajax.actionlink对象处理的并发访问方法将由上述AJAX链接被称为: -删除使用Ajax.actionlink
[HttpPost]
public void Delete(int id)
{ var a = repository.FindAnswer(id);
repository.DeleteAnswer(a);
repository.Save();}
和folloiwng的onSuccess脚本: -
function deleteconfirmation3(rid, rname) {
$('#' + rid).remove();
jAlert(rname + ' Was Deleted Succsfully succsfully', 'Deletion Confirmation');}
当前,如果两个用户访问相同的视图,然后他们都点击与同一对象关联的删除链接,则会在其中一个请求上引发空异常;所以我如何在动作方法和视图方面处理这个问题,以向用户显示友好的消息;在这两种情况下: -
- 要么当
var a = repository.FindAnswer(id);
返回空值 异常? - 或者当
repository.Save();
没有删除任何记录?
BR
编辑: -
我更新了帖子行动方法如下: -
[HttpPost]
public ActionResult Delete(int id)
{
try
{
Thread.Sleep(1000);
var a = repository.FindAnswer(id);
repository.DeleteAnswer(a);
repository.Save();
return Json(new { IsSuccess = "True", id = id, description = a.Description }, JsonRequestBehavior.AllowGet);
}
catch (ArgumentNullException) {
return Json(new { IsSuccess = "False" }, JsonRequestBehavior.AllowGet); }}
,并在视图我更新ajax.actionlink到folloiwng : -
@{ string i = "Are uou sure you want to delete " + @answer.Description.ToString() + " ?";}
@Ajax.ActionLink("Delete", "Delete", "Answer",
new { id = answer.AnswersID },
new AjaxOptions
{
//OnBegin = "deleteconfirmation1",
Confirm = i,
HttpMethod = "Post",
OnBegin = string.Format(
"disablelink({0})",
Json.Encode(answer.AnswersID)),
OnSuccess = "myresponse"
})
和的onSuccess脚本: -
function myresponse(data) {
if (data.IsSuccess == "True")
{
$('#' + data.id).remove();
jAlert(data.description + ' Was Deleted Succsfully succsfully', 'Deletion Confirmation');
}
else {
$('#' + data.id).remove();
jAlert(data.description + 'record has already been deleted', 'aaaa');
}
}
上面的代码工作时,我测试了罚款,,但这种方法听起来可以接受的,我没有写类似这样的东西之前? BR
我不认为这是一个点开发一些事务,以允许只通过一个浏览器窗口打开一个数据项。这个问题的目标是什么?并发问题总是与更新操作相关联而不是删除。如果用户尝试删除已删除的项目,则只会显示他的消息,这不会损害您的业务逻辑。 – 2012-03-14 22:06:17
在我的应用程序中,多个用户将有权删除同一个对象。因此向用户返回该对象成功删除的消息,即使该对象已被其他用户删除也可能会导致一些不一致问题.... – 2012-03-14 22:32:44