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

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

    You can check the name in the action as has been mentioned, but you might consider whether or not this is good design. It is a good idea to consider the responsibility of the action and not couple this design too much to UI aspects like button names. So consider using 2 forms and 2 actions:

    <% Html.BeginForm("Send", "MyController", FormMethod.Post); %>
    
    <% Html.EndForm(); %>
    
    <% Html.BeginForm("Cancel", "MyController", FormMethod.Post); %>
    
    <% Html.EndForm(); %>
    

    Also, in the case of "Cancel", you are usually just not processing the form and are going to a new URL. In this case you do not need to submit the form at all and just need a link:

    <%=Html.ActionLink("Cancel", "List", "MyController") %>
    

提交回复
热议问题