1
<div class=".parentForm'>
    @Ajax.BeginForm(... AjaxOptions { OnSuccess = "submitSuccess" })
    {...}
</div>

function postDone(result,two, three) {
   $('.parentForm').replaceWith(result);
}

I don't like this because if there are multiple forms from this partial they will all have the same class, and I would rather not come up with some funky scheme where I generate an ID on the server side and have to pass it around to the JS callbacks.

With .ajax I can use the this to get the form that submitted the request, then from there get the parent .formContainer. Is there any way to get a referrence to the form that submitted the form with OnSuccess?

Nick Albrecht
  • 16,607
  • 10
  • 66
  • 101
AaronLS
  • 37,329
  • 20
  • 143
  • 202

2 Answers2

2

Actually there is an easier way though you still have to modify the jquery.unobtrusive-ajax.js file. Add one line of code around line # 100. I provided the same answer here

options.data.push({ name: "X-Requested-With", value: "XMLHttpRequest" });

options.context = element; // <--- Add this line

method = options.type.toUpperCase();
if (!isMethodProxySafe(method)) {
    options.type = "POST";
    options.data.push({ name: "X-HTTP-Method-Override", value: method });
}

Update: AaronLS has created an issue on codeplex to see if they would make the change to the jQuery Unobtrusive Ajax file itself, upvote it if you would like this change added as well!

Codeplex Issue: Let 'this' reference the source element

Community
  • 1
  • 1
Nick Albrecht
  • 16,607
  • 10
  • 66
  • 101
  • +1 but hoping for a different workaround since this will get blown away everytime I update the file with a newer version. – AaronLS Feb 01 '13 at 22:37
  • I agree, I've looked all over for a way to submit this as a change request to Microsoft or whom ever maintains this file but in spite of it being easily available on Nuget, there is no other information for where the project lives or how to propose changes like this. I can tell you that they have not made any updates to the file recently, but that's still no substitution for an actual fix. – Nick Albrecht Feb 04 '13 at 16:18
  • They have an issues section on aspnet codeplex that covers mvc, there's also a uservoice.com ASP.NET MVC site, and then of course connect.microsoft.com. As far as submitting a patch, they explicitly state they don't except patches,etc. on the codeplex site. So while it is open source, it isn't open to contributors, AFAIK. – AaronLS Feb 04 '13 at 16:20
  • 1
    Yeah I'm familiar with those (though had forgotten about UserVoice), however I was unsure of using them because of being unsure who maintains the project (ASP.NET or a diff team). Maybe I'll give it a shot and see if they are open to the fix, worse case they don't make the change, which is where we're at now. Just saw your issue on CodePlex (+vote) :-) – Nick Albrecht Feb 04 '13 at 16:26
1

Of course, but you need to modify jquery.unobtrusive-ajax.js file. You must replace the following lines:

success: function (data, status, xhr) {
    asyncOnSuccess(element, data, xhr.getResponseHeader("Content-Type") || "text/html");       
    getFunction(element.getAttribute("data-ajax-success"), ["data", "status", "xhr"]).apply(this, arguments);
},

with:

success: function (data, status, xhr) {
    asyncOnSuccess(element, data, xhr.getResponseHeader("Content-Type") || "text/html");
    var args = $.merge(arguments, [element]);
    getFunction(element.getAttribute("data-ajax-success"), ["data", "status", "xhr"]).apply(this, args);
},

can access form in that way:

function submitSuccess(result, status, xhr, element) {
    // element is your form
}
Sławomir Rosiek
  • 4,038
  • 3
  • 25
  • 43
  • +1 but hoping for a different workaround since this will get blown away everytime I update the file with a newer version. – AaronLS Feb 01 '13 at 22:39