0

im working on a project using mvc asp.net and what i try to do now is to submit a form using javascript.

the javascript works and submit my form.

My problem is that i have a value in my button tag which i want get in my controller but its only null.

the controller is correct because if i do a normal submit without javascript i get the value i want from my <button>

using javascript i only receive null.

someone can give me a hand with this pls??

thanks in advance

Here is my code:

 $(function () {
        $('form').find('button[type=submit]').click(function (e) {
            e.preventDefault();
            $form = $(this).closest('form');
            if (this.name == 'yes') {
                doConfirm("Confirm that you want to validate?", function yes() {
                    $form.submit();
                });
            } else {
                doReject("Confirm that you want reject?", function no() {
                    // do nothing
                });
            }
        });
    });
4
  • 2
    Can you please add the HTML code?
    – Babblo
    Commented Nov 20, 2013 at 15:38
  • where you placed the button? inside form? Commented Nov 20, 2013 at 15:39
  • hey guys.. see my update html code above pls Commented Nov 20, 2013 at 15:40
  • Which button value do you need in the controller?
    – Kami
    Commented Nov 20, 2013 at 15:42

1 Answer 1

1

Programmatically submitted forms don't include a parameter corresponding to a submit button, which makes sense considering they weren't directly triggered by clicking a button. The easiest way would be to add the corresponding parameter to the action property of the form prior to submitting it. Something like this should work:

var button = this;
...
doConfirm("Confirm that you want to validate?", function yes() {    
    $form[0].action += ($form[0].action.indexOf('?') != -1 '&' : '?') + button.name + '=' + button.value;
    $form.submit();
}

Alternatively you could turn the entire thing into an AJAX submit, and include the corresponding parameter along with the other form data.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.