.net Mvc 3 Ajax.BeginForm, get the form element

Phil Cooper picture Phil Cooper · Jul 30, 2011 · Viewed 7.9k times · Source

I'm trying to work with the form dom element with the OnBegin & OnComplete routines of the Ajax.BeginForm helper in Mvc.

Currently I have this:

@using (Ajax.BeginForm("Contact", "Home", new AjaxOptions { OnBegin = "handleOnBegin" }))

But within the OnBegin / OnComplete handlers, I want to work with the form dom element - is this going to be possible? I've checked the arguments passed to those handlers and I can't see anything.

function handleOnBegin(a, b){
  var f = <get form>;
  animateForm(f);
}

I've even tried passing 'this' with the handlers but that only seems to pass the XHR object (or something similar)... Also, I'm reluctant to tit about with passing id's and adding more code as I'm convinced there's a simpler way.

Answer

Steve Morgan picture Steve Morgan · Jul 30, 2011

An an ID to your form as the 4th parameter to Ajax.BeginForm:

@using (Ajax.BeginForm("Contact", "Home", 
        new AjaxOptions { OnBegin = "forms.onBegin", OnComplete = "forms.onComplete" },
        new { id = "FormName" }))

Then you can select the form by ID (#FormName).

Edit:

Or see the answers to this question