jQuery not submitting my form to an asp.net mvc controller

I'm trying to submit a form via ajax to an MVC controller.

HTML
<% using (Html.BeginForm("AskQuestion", "Home", FormMethod.Post, new { id="submitquestion"})) {%>
jQuery
$("#submitquestion").submit(function(event) {
    event.preventDefault();
    var form = $(this);
    $.ajax({
        url: '<%= Url.Action("AskQuestion", "Home") %>',
        type: "Post",
        data: form.serialize(),
        success: function(result) {
            if (result.success) {
                //success method
            }
        }
    });

I'm getting no javascript errors, and my controller is not getting hit when I set a breakpoint. However, if I just set this:

$("#submitquestion").submit(); 

The form submits.

What am I doing wrong? I want to submit the form via .ajax

Answers


Add new html button to submit and wirte your ajax submit in the click event like this,

    $("#yourButton").click(function(event) {
        event.preventDefault();
        var form = $('#submitquestion');
        $.ajax({
            url: '<%= Url.Action("AskQuestion", "Home") %>',
            type: "Post",
            data: form.serialize(),
            success: function(result) {
                if (result.success) {
                    //success method
                }
            }
        });
});

Need Your Help

Can a GDBM database created in C be opened in Python?

python c osx magic-numbers gdbm

I have a Python program in which I am trying to use this rhyming dictionary to look up rhymes.

can we call some part of native in HTML

cordova worklight

I have developed native ios carousal and some graph page which is in native. I have common header and footer for my application which i want throughout the application.