Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

migrating from microsoft Ajax.BeginForm to jquery

i have slowly been moving from microssoft ajax to jquery for all my ajax stuff. the only thing i have left is some forms that are posting over using microsoft ajax by using Ajax.Beingform. This is an asp.net mvc site so submit should call a controller post action.

What is the simplest way to convert this to jquery so i can remove my reference to microsoft ajax.

like image 665
leora Avatar asked Feb 27 '10 21:02

leora


1 Answers

It's probably easiest to use the jQuery Form plugin to achieve most of this functionality if you had a form that looks like:-

<%= Ajax.Form(new AjaxOptions {
         Url = theUrl,
         Method = theMethod,
         Confirm = confirmFunction, 
         InsertionMode = InsertionMode.Before,
         OnBegin = onBegin,
         OnComplete = onComplete,
         OnFailure = onFailure,
         OnSuccess = onSuccess,
         UpdateTargetId = elementId,
         LoadingElementId = loadingElementId
         });

This would correspond to a form plug-in call of:-

$("#yourFormId").ajaxForm({
    url : theUrl,
    type : theMethod,
    beforeSubmit : confirmFunction,
    beforeSend : onBegin,
    complete : onComplete,
    success : onSuccess,
    error : onFailure
});

The only issues are replicating the LoadingElementId, UpdateTargetId and InsertionMode properties.

If you want to replicate InsertionMode.Replace you can pass the additional target option to the ajaxForm plug-in. If you want to replicate the remaining functionality you'd have to write your own beforeSend, success and complete event handlers.

Something like the following would simulate a form with InsertionMode.Before, UpdateTargetId = "Test", LoadingElementId = "Loader":-

$("#yourFormId").ajaxForm({
     beforeSend : function() { $("#Loader").show(); },
     complete : function() { $("#Loader").hide(); },
     success: function(result) { $(result).prependTo("#Test"); }
});
like image 106
John Foster Avatar answered Oct 06 '22 21:10

John Foster