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

后端 未结 30 3002
一个人的身影
一个人的身影 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:47

    I've came across this 'problem' as well but found a rather logical solution by adding the name attribute. I couldn't recall having this problem in other languages.

    http://www.w3.org/TR/html401/interact/forms.html#h-17.13.2

    • ...
    • If a form contains more than one submit button, only the activated submit button is successful.
    • ...

    Meaning the following code value attributes can be changed, localized, internationalized without the need for extra code checking strongly-typed resources files or constants.

    <% Html.BeginForm("MyAction", "MyController", FormMethod.Post); %>
    
    
    
    <% Html.EndForm(); %>`
    

    On the receiving end you would only need to check if any of your known submit types isn't null

    public ActionResult YourAction(YourModel model) {
    
        if(Request["send"] != null) {
    
            // we got a send
    
        }else if(Request["cancel"]) {
    
            // we got a cancel, but would you really want to post data for this?
    
        }else if(Request["draft"]) {
    
            // we got a draft
    
        }
    
    }
    

提交回复
热议问题