获取的onSuccess调用形式形式、onSuccess

2023-09-10 20:52:41 作者:不痛不痒不在乎丶

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

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

我不喜欢这一点,因为如果有这一部分,他们都会有相同的类多种形式的,我宁可不拿出一些时髦的计划,我产生在服务器端的ID,并必须通过它周围的JS回调。

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.

使用我可以使用阿贾克斯的来获取提交请求,然后从那里获取父.formContainer形式。有没有什么办法让一个参鉴到与的onSuccess提交表单的形式?

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?

推荐答案

其实还有一个更简单的方法,虽然你还是要修改 jquery.unobtrusive-ajax.js 文件。添加一行行左右#100 code我提供同样的答案here

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 });
}

更新:AaronLS创造了在codePLEX一个问题,看看他们是否会做出改变了jQuery不显眼的Ajax文件本身,upvote它,如果你想这个变化增加以及!

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问题:我们这个'引用源元素