views:

50

answers:

1

Hi, I got control with strongly typed View, with Ajax.BeginForm(). Now I would like to change submit method from

<input type="submit" id="testClick" value="Submit" />

To some javascript method DoSubmit(). What I tried is :

  • Invoke click on that submit button
  • Invoke submit on form ('form1').submit(), document.forms['form1'].submit()
  • jQuery forms with ('form1').AjaxSubmit();
  • Create jQuery AJAX

    $.ajax({                                                        
        type: "POST",
        url: $("#form1").attr("action"),
        data: $("#form1").serialize(),            
        success: function() {
            alert("epic win!!!1!1!")      
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            alert("epic fail!")                                        
        }
    });
    

All those method created normal request (not AJAX), or they didn't work. So anyone know how I can do AJAX submit "Form", from JavaScript and strongly typed mechanism (public AcrionResult MyFormAction(FormModel model); ) will work?

+1  A: 

I've had this work fine for me using the forms plugin for jquery. What I found tho, was that I had to handle the click event, do the ajax submit and then return false to ensure that the normal post didn't occur.

<script type="text/javascript">
    $('#testClick').click(function(){
        $('#form1').ajaxSubmit();
        return false;
    });
</script>
lomaxx
Thanks for answer it work fine. But there is one problem after that ajax submit form isn't validate. I'm using Xval and jquery.validation. I tried to run : $('#form1').validate(); after ajaxSubmit or before and it didn't work
ANDyW