I\'m wondering how you might go about implementing multiple form actions when submitting a form in asp.net mvc 3 RC.
If I\'m editing a user, for example I would like to
The suggestion by @Omar is great. Here is how I made this a little more generic in the case where I wanted a confirmation when the user is prompted to delete an object. Note! in the HttpPost I'm pulling the object again rather than using the item passed to the method. You can reduce a DB call by the having the view contain all the properties so "Item" is populated.
Here's the View Model
public class DeleteViewModel {
public string ActionType { get; set; }
public T Item { get; set; }
}
Controller
public ActionResult Delete(int id) {
DeleteViewModel model = new DeleteViewModel() {
Item = categoryRepository.Categories.FirstOrDefault(x => x.CategoryID == id)
};
return View(model);
}
[HttpPost]
public ActionResult Delete(DeleteViewModel model) {
if (model.ActionType == "Cancel")
return RedirectToAction("Index");
else if (model.ActionType == "Delete") {
var cat = categoryRepository.Categories.FirstOrDefault(x => x.CategoryID == model.Item.CategoryID);
categoryRepository.Delete(cat);
return RedirectToAction("Index");
}
//Unknown Action
return RedirectToAction("Index");
}
View