How do you handle multiple submit buttons in ASP.NET MVC Framework?

后端 未结 30 3029
一个人的身影
一个人的身影 2020-11-21 07:16

Is there some easy way to handle multiple submit buttons from the same form? For example:

<% Html.BeginForm(\"MyAction\", \"MyController\", FormMethod.Pos         


        
30条回答
  •  遇见更好的自我
    2020-11-21 07:27

    I've tried to make a synthesis of all solutions and created a [ButtenHandler] attribute that makes it easy to handle multiple buttons on a form.

    I've described it on CodeProject Multiple parameterized (localizable) form buttons in ASP.NET MVC.

    To handle the simple case of this button:

    
    

    You'll have something like the following action method:

    [ButtonHandler()]
    public ActionResult AddDepartment(Company model)
    {
        model.Departments.Add(new Department());
        return View(model);
    }
    

    Notice how the name of the button matches the name of the action method. The article also describes how to have buttons with values and buttons with indexes.

提交回复
热议问题