0

I have a problem about how to use 2 actions which must share an value in a view which contains 2 submit buttons. In a "Delete" view, I want to have to action : delete the person or desactivate the person (desactivate means assigning an end date to his contract).

Here is my submit buttons :

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

</p>

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

}

And there are my actions :

    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");
    }

I tried to separate my submit button into different forms but it didn't work and it's normal because I need to use the same id for the delete action and the desactivate action.

Any idea?

Traffy
  • 2,803
  • 15
  • 52
  • 78
  • change the action of the form on the click of respected button and you can get it done. – K D Mar 14 '13 at 10:21
  • Thanks for your answer but I didn't really catch what you meant. Should I operate my changes in the view? – Traffy Mar 14 '13 at 10:26
  • well just change the action of your form from the view, and rest is already implemented. One more thing you can do is just remove the Deactivate action, take one hidden input field which will track which button is clicked, and on the basis of that value perform the operation in the controller.. but the solution i proposed would work efficiently for you. hence better to go with the existing one – K D Mar 14 '13 at 10:37
  • Check [this answer](http://stackoverflow.com/a/8258799/1199711). – Zabavsky Mar 14 '13 at 10:42

1 Answers1

0

Try this

@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>
K D
  • 5,889
  • 1
  • 23
  • 35
  • Unfortunatelly it didn't work... When I click on the desactivate button, it deletes my person. – Traffy Mar 14 '13 at 10:37