基本上,除了我使用的是MVCRazor外,我的问题与该问题相似甚至重复。而且我确定那里的答案已经过时了,因为当前使用的客户端库是jQuery /不显眼的Ajax。
因此,总而言之,我正在尝试OnSuccess在提供的属性中指定的处理程序中访问触发Ajax请求的锚元素AjaxOptions。
OnSuccess
AjaxOptions
这是ActionLink:
ActionLink
@Ajax.ActionLink("Add opening times entry", "AddOpeningTimes", new { htmlPrefix = Html.HtmlPrefixFor(m => Model.OpeningTimes) }, new AjaxOptions { UpdateTargetId = "openingTimes", InsertionMode = nsertionMode.InsertAfter, OnSuccess = "updateHtmlPrefix" }, new { title = "Add opening times entry" })
JS:
function updateHtmlPrefix() { this.href = this.href.replace(/\d(?=]$)/, function (i) { return ++i; }); }
你总是可以写
OnBegin="function() { clickedLink = $(this); }"
然后,您可以在成功处理程序中访问clickedLink变量(请记住使用页面范围对其进行声明)。
编辑:
在对调用堆栈进行一些操作之后,您可以尝试如下操作:
<script type="text/javascript"> function start(xhr) { var stack = start.caller; // walk the stack do { stack = stack.caller; } while (stack.arguments != undefined && stack.arguments.length > 0 && (stack.arguments[0].tagName == undefined || stack.arguments[0].tagName != "A")) //stack now points to the entry point into unobtrusive.ajax if (stack.arguments != undefined) xhr.originalElement = $(stack.arguments[0]); //blech } function UpdateHrefText(result, status, xhr) { debugger; if(xhr.originalElement != undefined) xhr.originalElement.text(result.Message); } </script> @Ajax.ActionLink("Test", "Message", "Home", new AjaxOptions{ OnBegin = "start", OnSuccess = "UpdateHrefText"})
虽然不确定我会在生产中相信这一点。我会做更多类似的事情:
<script type="text/javascript"> var theLink; function start(xhr) { xhr.originalElement = theLink; } function UpdateHrefText(result, status, xhr) { debugger; if(xhr.originalElement != undefined) xhr.originalElement.text(result.Message); } </script> @Ajax.ActionLink("Test", "Message", "Home", null, new AjaxOptions{ OnBegin = "start", OnSuccess = "UpdateHrefText"}, new { onclick="theLink = $(this);"})