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

后端 未结 2 1518
别那么骄傲
别那么骄傲 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条回答
  • 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<T> {
        public string ActionType { get; set; }
        public T Item { get; set; }
    }
    

    Controller

        public ActionResult Delete(int id) {
            DeleteViewModel<Category> model = new DeleteViewModel<Category>() {
                Item = categoryRepository.Categories.FirstOrDefault(x => x.CategoryID == id)
            };
            return View(model);
        }
    
        [HttpPost]
        public ActionResult Delete(DeleteViewModel<Category> 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

        <div class="actions">
            <div class="actions-left"><input type="submit" value="Cancel" name="ActionType"/></div>
            <div class="actions-right"><input type="submit" value="Delete" name="ActionType" /></div>
        </div>   
    
    0 讨论(0)
  • 2021-02-13 14:11

    You can have multiple submit buttons in a form with the same name attribute but different value attributes. Which ever button is clicked, the associated value will be posted to the server.

    You can use a simple link for Cancel but I'll include it as a button anyway.

    <input type="submit" name="actionType" value="Save" />
    <input type="submit" name="actionType" value="Save and Close" />
    <input type="submit" name="actionType" value="Cancel" />
    

    And in your action, test for the values.

    public ActionResult Edit(string actionType)
    {
        if(actionType == "Save")
        {
            // Save action
        }
        else if (actionType == "Save and Close")
        {
            // Save and quit action
        }
        else
        {
            // Cancel action
        }
    }
    

    If you don't like having the long text in the value attribute, you can use standard HTML <button> tag which lets you define a separate value and separate text.

    0 讨论(0)
提交回复
热议问题