2012-04-25 24 views
0

我有顯示對象的列表和每個對象旁邊一個刪除Ajax.actionlink的folloiwng索引視圖: -如何傳遞時間戳與我Ajax.actionlink

<legend>@Model.Where(d => d.VisitStatu.Description.ToUpper().Equals("ASSINGED")).Count() @ViewBag.subtitle</legend> 
<table> 
    <tr> 
     <th>Patient Full Name </th> 

     <th>Created BY</th> 
     <th></th> 
    </tr> 

@foreach (var item in Model.Where(d => d.VisitStatu.Description.ToUpper().Equals("ASSINGED"))) 
{ 
    <tr id = @item.VisitID>   
     <td>@Html.DisplayFor(modelItem => item.Patient.FullName)</td> 
     <td>@Html.DisplayFor(modelItem => item.CreatedBy)</td> 
     <td>@Ajax.ActionLink("Delete", 
     "Delete", "Visit", 
     new { id = item.VisitID }, 
    new AjaxOptions 
{ 
    HttpMethod = "Post", 
    OnSuccess = "deletionconfirmation", 
    OnFailure = "deletionerror" 
} 
</td> </tr> 

其中Ajax.actionlink會請撥打以下POST操作方法: -

 [HttpPost] 
     public ActionResult Delete(int id) 
     {   try 
      { var v = repository.GetVisit(id); 
       if (!(v.Editable(User.Identity.Name))) 
       {return View("NotFound");    } 
       repository.DeleteVisit(v); 
       repository.Save(); 
return Json(new { IsSuccess = "True", id = id, description = v.Date.ToString() }, JsonRequestBehavior.AllowGet); 
      } 
      catch (DbUpdateConcurrencyException) 
      { 
return Json(new { IsSuccess = "False" }, JsonRequestBehavior.AllowGet); 

      } 
      catch (OptimisticConcurrencyException) 
      { 
       return Json(new { IsSuccess = "False" }, JsonRequestBehavior.AllowGet); 

      }} 

但目前,如果用戶點擊刪除鏈接,該紀錄是由其他用戶則沒有DbUpdateConcurrencyException將提高修改,,因爲我沒有通過時間戳的刪除行動方法...所以我如何包括時間amp的值與刪除ajax.actionlink? //提示該對象已包含timestamp屬性。 BR

回答

1

你可以把它作爲查詢字符串參數,您傳遞的ID以同樣的方式:

@Ajax.ActionLink(
    "Delete", 
    "Delete", 
    "Visit", 
    new { 
     id = item.VisitID, 
     timestamp = item.Timestamp 
    }, 
    new AjaxOptions 
    { 
     HttpMethod = "Post", 
     OnSuccess = "deletionconfirmation", 
     OnFailure = "deletionerror" 
    } 
) 

,然後在這個刪除控制器動作採取timestamp參數。

+0

感謝您的回覆,但是我如何告訴Post操作方法在發送到數據庫的刪除請求中包含時間戳? – 2012-04-26 11:59:32

+0

@johnG,這個問題與ASP.NET MVC不再有任何關係。請開始一個新的線程,在那裏你解釋你的數據庫結構,你正在使用的數據訪問技術,如果你正在使用一個ORM如EF來顯示你的模型類...... – 2012-04-26 13:55:37