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

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

    Eilon suggests you can do it like this:

    If you have more than one button you can distinguish between them by giving each button a name:

    
    
    

    In your controller action method you can add parameters named after the HTML input tag names:

    public ActionResult DoSomeStuff(string saveButton, string
    cancelButton, ... other parameters ...)
    { ... }
    

    If any value gets posted to one of those parameters, that means that button was the one that got clicked. The web browser will only post a value for the one button that got clicked. All other values will be null.

    if (saveButton != null) { /* do save logic */ }
    if (cancelButton != null) { /* do cancel logic */ }
    

    I like this method as it does not rely on the value property of the submit buttons which is more likely to change than the assigned names and doesn't require javascript to be enabled

    See: http://forums.asp.net/p/1369617/2865166.aspx#2865166

提交回复
热议问题