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?