2012-03-14 62 views
0

我有我的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');} 

当前,如果两个用户访问相同的视图,然后他们都点击与同一对象关联的删除链接,则会在其中一个请求上引发空异常;所以我如何在动作方法和视图方面处理这个问题,以向用户显示友好的消息;在这两种情况下: -

  1. 要么当var a = repository.FindAnswer(id);返回空值 异常?
  2. 或者当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

+1

我不认为这是一个点开发一些事务,以允许只通过一个浏览器窗口打开一个数据项。这个问题的目标是什么?并发问题总是与更新操作相关联而不是删除。如果用户尝试删除已删除的项目,则只会显示他的消息,这不会损害您的业务逻辑。 – 2012-03-14 22:06:17

+0

在我的应用程序中,多个用户将有权删除同一个对象。因此向用户返回该对象成功删除的消息,即使该对象已被其他用户删除也可能会导致一些不一致问题.... – 2012-03-14 22:32:44

回答

1

添加一个检查,以便如果记录存在,删除它,否则显示一条消息,指明该记录已被删除...

[HttpPost] 
public ActionResult Delete(int id) 
{  var a = repository.FindAnswer(id); 
     if(/*check if a is not null*/){ 
     repository.DeleteAnswer(a); 
     repository.Save(); 
     return Json(new{ IsSuccess="True", id=id,description=a.Description }); 
     }else{ 
      // display a message record already been deleted 
      return Json(new{ IsSuccess="False" }); 
     }  

} 

apparantly你将需要改变的返回类型从无效到ActionResultJsonResult

@Ajax.ActionLink("Delete", "Delete", "Answer", 
     new { id = answer.AnswersID }, 
      new AjaxOptions 
      { 
       Confirm = i, 
       HttpMethod = "Post", 
       OnBegin = string.Format(
         "disablelink({0})", 
         Json.Encode(answer.AnswersID)), 
       OnSuccess = "myResponse" 
      }) 

的成功处理程序会像

function myResponse(data){ 
if(data.IsSuccess=="True") 
    jAlert(data.description + ' Was Deleted Succsfully succsfully', 'Deletion Confirmation'); 
    else { 
    alert("record has already been deleted"); 
    } 

}

+0

感谢您的回复。但是,如果我在上述帖子中提到的Post操作方法上面检查了“if(/ * check a not not null * /)”,那么两个用户可能会得到相同的结果,即“a”不为null在同一时间点击“删除”,所以问题仍然会发生?我了解代码逻辑错误吗? – 2012-03-14 22:43:12

+0

看我的更新。 “而不是执行检查后的行动uisng”if(/ *检查是否不是空* /)“”,我返回不同的Json数据;一个关于Try和其他关于Catch(ArgumentNullException)。可接受?? – 2012-03-15 00:04:15

+0

无论何时用户同时点击同一个删除链接一个请求将首先到达服务器所以第二个(不是最快的)请求将获得已删除的记录 – JIA 2012-03-15 05:27:17