2013-03-14 90 views
0

我有一個關於如何使用2個操作的問題,必須在包含2個提交按鈕的視圖中共享一個值。在「刪除」視圖中,我想要採取行動:刪除人員或取消激活人員(取消激活意味着將合約的結束日期分配給他)。多個提交按鈕與共享相同信息的兩個操作

這裏是我的提交按鈕:

@using (Html.BeginForm()) { 
<p> 
    <input type="submit" value="Delete"/> 
    <input type="submit" value="Desactivate" /> 

</p> 

@Html.ActionLink("Back to List", "Index") 

}

而且有我的行動:

public ActionResult Delete(long id = 0) 
    { 
     Person person = db.Persons.Single(p => p.Id_Person == id); 
     if (person == null) 
     { 
      return HttpNotFound(); 
     } 


     return View(person); 
    } 

    // 
    // POST: /Person/Delete/5 

    [HttpPost, ActionName("Delete")] 
    public ActionResult DeleteConfirmed(long id) 
    { 
     Person person = db.Persons.Single(p => p.Id_Person == id); 
     db.Persons.DeleteObject(person); 
     db.SaveChanges(); 
     return RedirectToAction("Index"); 
    } 


    [HttpPost] 
    public ActionResult Desactivate(long id) 
    { 
     Person person = db.Persons.Single(p => p.Id_Person == id); 

     person.EndDate = DateTime.Now; 

     db.Persons.Attach(person); 
     db.ObjectStateManager.ChangeObjectState(person, EntityState.Modified); 
     db.SaveChanges(); 

     return RedirectToAction("Index", "Person"); 
    } 

我試圖分開我的提交按鈕成不同的形式,但它並沒有工作,這是正常的,因爲我需要使用相同的ID刪除行動和取消行動。

有什麼想法?

+0

變化形式的尊重上點擊按鈕的動作,你可以把它完成。 – 2013-03-14 10:21:49

+0

感謝您的回答,但我沒有真正瞭解您的意思。我應該在視圖中操作我的更改嗎? – Traffy 2013-03-14 10:26:40

+0

以及只是從視圖中更改表單的動作,並且休息已經實現。還有一件事你可以做的就是刪除停用動作,取一個隱藏的輸入字段,它將跟蹤哪個按鈕被點擊,並根據該值執行控制器中的操作..但我提出的解決方案將有效地工作您。因此更好地去與現有的 – 2013-03-14 10:37:41

回答

0

試試這個

@using (Html.BeginForm()) { 
<p> 
    <input type="submit" class="delete" value="Delete"/> 
    <input type="submit" class="deactivate" value="Desactivate" /> 

</p> 

    @Html.ActionLink("Back to List", "Index") 
    } 
<scirpt type="text/javascript"> 
$(function(){ 
$(".delete").click(function(){ 
    $(this).parents("form").action = "ControllerName/DeleteConfirmed"; 
    return true; 
}); 

$(".deactivate").click(function(){ 
    $(this).parents("form").action = "ControllerName/Desactivate"; 
    return true; 
}); 

}); 
</script> 
+0

不幸的是它沒有工作......當我點擊取消激活按鈕時,它刪除了我的人。 – Traffy 2013-03-14 10:37:58