Alternatively, use 2 separate action-links or 2 forms. Something I don't like about ActionSelectName is that IsValidName is called for every action method in the controller; I don't know why it works this way.
I like a solution where every button has a different name based on what it does, but I don't like the fact that you have to have as many parameters in the action method as buttons in the form.
I have created an enum for all button types:. The filter will find the button name in the form data and if the button name matches any of the button types defined in the enum, it will find the ButtonType parameter among the action parameters:.
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. Meaning the following code value attributes can be changed, localized, internationalized without the need for extra code checking strongly-typed resources files or constants.
On the receiving end you would only need to check if any of your known submit types isn't null. In case you are a Ajax lover this second option would excite you more. Each of these function make call to respective action names. This is a great and a clean option. This function is where we write the logic whether an action has to be executed or not. So if this function returns true then the action is executed or else it is not.
The main heart of the above function is in the below code. So if the submit is hitting this action and if the name matches of the HTML submit button name it then executes the action further or else it does not.
Read the article multiple buttons in the same form to see his solutions, and the advantages and disadvantages of each. IMHO he provides a very elegant solution which makes use of attributes that you decorate your action with.
This is the technique I'd use and I don't see it here yet. It adapts Dylan Beattie's answer to do localization without any problems. This script allows to specify a data-form-action attribute which will work as the HTML5 formaction attribute in all browsers in an unobtrusive way :.
The form containing the button will be posted to the URL specified in the data-form-action attribute:. This requires jQuery 1. For previous versions you should use live instead of on.
Here is the extension method you call from the controller with form. For image buttons it looks for a form parameter with. I prefer this way becuase it means you can change the text display value without having to change the code. My buttons say different things under different contexts - but map to the same 'command'. I don't have enough rep to comment in the correct place, but I spent all day on this so want to share.
GetValue keyValue would incorrectly come back null. It turned out I was referencing System. MVC version 3. I don't know why my project didn't upgrade correctly and I had no other obvious problems. I'm pretty late to the party, but here goes My implementation borrows from mkozicki but requires less hardcoded strings to get wrong. Framework 4. Essentially, the controller method name should be the key to the routing.
The button name must be keyed with "action:[controllerMethodName]". Attribute Magic. Notice the use of CallerMemberName goodness. 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. 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. To see if this is a problem with your current site, open the your form in a window and just before you go to click Save or Publish , open a duplicate window, and logout.
Now go back to your first window and try to submit your form using either button. For me I got an error so this change solves that problem for me. I omit a bunch of stuff for the sake of brevity but you should get the idea. The key parts are the inclusion of ActionName on the attribute and making sure the name passed in is the name of the View that shows the form. Based on mkozicki answer I come up with a bit different solution. They do almost the same so I didn't want to have two actions.
I also want to point out that if actions do different things I would probably follow mkozicki post. I've created an ActionButton method for the HtmlHelper. These methods have various parameters, but for the ease of use you can create some overload that take just the parameters you need.
Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. How do you handle multiple submit buttons in ASP. Ask Question. Asked 12 years, 10 months ago. Active 1 year, 8 months ago. Viewed k times. Is there some easy way to handle multiple submit buttons from the same form?
Improve this question. Spoike Spoike k 42 42 gold badges silver badges bronze badges. I have not examined them; nevertheless, there's a good chance Google is using Ajax; problem is with Google, you will not be able to see what they are doing on the server side.
I just quickly scanned the postings, but sounds the "problem" is mulitple submit buttons on a form? The content you requested has been removed.
Ask a question. Quick access. Search related threads. Remove From My Forums. Answered by:. Archived Forums. Sign in to vote. User posted How to place 2 submit and handle it separately Pls help me in this i am new to MVC Thanks in advance..
Monday, April 27, AM. Monday, April 27, PM. User posted Hi You can do that, but you will need to write some JavaScript to handle this. You could try something like the following Let's assume this is in Index. You can get more information at aspnet 5 documentation page. After further investigation I found that this is a known issue. Short answer : No there's no way to know which button was pressed Long answer : depending on your model and your change listener, if you do a comparison between the new value and the previous value, it is possible to know if the user went forward or backward.
Only authorized users can answer the question. Please sign in first, or register a free account. If the target browser supports HTML5 all the leading browsers support these attributes then using formaction and formmethod attributes is recommended. This way you get a cleaner separatation of the concerns and the code is more readable. If the form or the buttons themselves are being generated on-the-fly then jQuery technique is handy because things are changing dynamically.
Online Courses ASP. Multiple buttons with different names In this technique you use BeginForm helper as usual and submit a form to an action method. The relevant markup from the Index view that does this is shown below: model MultipleSubmitButtons. The markup of Result view is shown below: model MultipleSubmitButtons. Multiple buttons with the same name This technique is similar to the previous technique. The modified form markup is shown below: using Html.
The following markup shows how the modified form looks like: using Html. Which technique to use? Bipin Joshi is an independent software consultant, trainer, author, and meditation teacher. He conducts instructor-led online training courses in ASP. NET family of technologies for individuals and small groups.
He is a published author and has authored or co-authored books for Apress and Wrox press. Having embraced the Yoga way of life he also teaches Ajapa Yoga to interested individuals. To know more about him click here. Popular from this category Load ASP. All rights reserved. About Us. NET Online Courses.
0コメント