ASP.NET MVC 2:防止ajax操作链接替换updateTarget
我在视图上使用 ajax 操作链接,然后将 js 函数绑定到其 onCompleted 属性上。 在此函数中,我获取响应对象,做一些有趣的事情,然后将消息属性写入 updatetarget 元素。
问题是,当它完成 oncompleted 事件的工作时,它将原始 json 响应写入 updatetarget 元素,替换我已经编写的文本。我想阻止它将原始响应写入更新目标。我知道 InsertionMode 属性,但它对我来说毫无用处,因为它以某种方式将文本附加到元素。
我提到的脚本如下;
查看操作链接的代码:
<%: Ajax.ActionLink("Delete", "Delete",
new { id = Model.Id, secretKey = Model.SecretKey },
new AjaxOptions { OnComplete = "WriteJsonResultToElement", UpdateTargetId="commandResult" })
%>
WriteJsonResultToElement 函数
function WriteJsonResultToElement(resultObject) {
updateTarget = resultObject.get_updateTarget();
obj = resultObject.get_object();
$(updateTarget).text(obj.message); // here i set the text of update target
if (obj.result > 0)
$('*:contains("' + obj.id + '")').last().parent().remove();
}
我的 JsonResult Delete 方法在操作后返回此数据:
{"message":"Deleted","result":1,"id":132}
谢谢。
I use an ajax action link on a view, then bind a js function onto its onCompleted property.
In this function, i get the response object, do some funny stuff, then write the message property to the updatetarget element.
The problem is, when it finishes its work on the oncompleted event, it writes the raw json response onto the updatetarget element, replacing the text i already written. I want to prevent it to write the raw response to the updatetarget. I'm aware of the InsertionMode property, but its useless to me because it appends text to the element one way or another.
The scripts i mentioned are below;
The code of the action link on view:
<%: Ajax.ActionLink("Delete", "Delete",
new { id = Model.Id, secretKey = Model.SecretKey },
new AjaxOptions { OnComplete = "WriteJsonResultToElement", UpdateTargetId="commandResult" })
%>
The WriteJsonResultToElement function
function WriteJsonResultToElement(resultObject) {
updateTarget = resultObject.get_updateTarget();
obj = resultObject.get_object();
$(updateTarget).text(obj.message); // here i set the text of update target
if (obj.result > 0)
$('*:contains("' + obj.id + '")').last().parent().remove();
}
My JsonResult Delete method returns this data after action:
{"message":"Deleted","result":1,"id":132}
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您不希望将原始 JSON 响应附加到 DOM,请不要指定
UpdateTargetId
:并在成功回调中处理它:
If you don't want the raw JSON response appended to the DOM don't specify an
UpdateTargetId
:and handle it in the success callback: