How to best implement Save | Save and Close | Cancel form actions in ASP.NET MVC 3 RC

后端 未结 2 1515
别那么骄傲
别那么骄傲 2021-02-13 13:33

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

2条回答
  •  梦毁少年i
    2021-02-13 13:59

    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

        

提交回复
热议问题