Ajax.ActionLink AjaxOptions 回调问题 (ASP.NET MVC 3)

发布于 2024-11-03 20:37:39 字数 873 浏览 5 评论 0原文

我在 PartialView 中有一个 Ajax.ActionLink ,如下所示:

@Ajax.ActionLink(Model.IsVisible ? "Disable" : "Enable", "ToggleVisibility", "Review", new { area = "Admin", id = Model.Id }, new AjaxOptions { HttpMethod = "POST", OnComplete = "onComplete_AdminReviewOption" })

处理 JavaScript 函数(目前在主视图上内嵌声明):

function onComplete_AdminReviewOption(ajaxContext) {
    var jsonObject = ajaxContext.get_object();
}

抛出一个 javascript 错误:

Object# 没有 get_object() 的定义

我认为这些 JavaScript 方法是 MicrosoftAjax.js / MicrosoftMvcAjax.js 脚本的一部分,这两个脚本我都包含在我的页面上。

任何人都可以确认这些辅助方法存在哪个库吗?

我在 Layout.cshtml 文件中加载所需的脚本,然后执行 AJAX 调用来渲染上面的 PartialView。

因此,当我处理该函数时,库已经加载 - 这就是我感到困惑的原因。

有什么想法吗?

I have an Ajax.ActionLink inside a PartialView like so:

@Ajax.ActionLink(Model.IsVisible ? "Disable" : "Enable", "ToggleVisibility", "Review", new { area = "Admin", id = Model.Id }, new AjaxOptions { HttpMethod = "POST", OnComplete = "onComplete_AdminReviewOption" })

And the handling JavaScript function (declared in-line on the main View for now):

function onComplete_AdminReviewOption(ajaxContext) {
    var jsonObject = ajaxContext.get_object();
}

Which throws a javascript error:

Object# has not definition for get_object().

I thought these JavaScript methods were part of the MicrosoftAjax.js / MicrosoftMvcAjax.js scripts, both of which i have included on my page.

Can anyone confirm which library these helper methods are present?

I load the required scripts in my Layout.cshtml file, then i do an AJAX call to render out the above PartialView.

So by the time i handle that function, the libraries are already loaded - which is why im confused.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我恋#小黄人 2024-11-10 20:37:39

您似乎正在使用 ASP.NET MVC 3 和 Razor。在此版本中,jQuery 是默认的客户端脚本框架。不再有 MicrosoftAjax.js (感谢上帝)。所以:

function onComplete_AdminReviewOption(ajaxContext) {
    var jsonObject = eval(ajaxContext);
}

也不要忘记包含jquery.unobtrusive-ajax.js

如果您想使用旧的东西,您可以通过在 web.config 中设置以下内容:

<add key="UnobtrusiveJavaScriptEnabled" value="false" />

默认情况下,此变量设置为 true。

就我个人而言,我更喜欢使用标准链接:

@Html.ActionLink(
    Model.IsVisible ? "Disable" : "Enable",  // <-- this should probably be as a property directly in the view model, ex. Model.LinkText
    "ToggleVisibility", 
    "Review", 
    new { area = "Admin", id = Model.Id }, 
    new { id = "myLink" }
)

并在单独的 javascript 文件中使用 jQuery 对它们进行 AJAXify:

$(function() {
    $('#myLink').click(function() {
        $.post(this.href, function(result) {
            // result is already a javascript object, no need to eval
        });
        return false;
    });
});

It looks like you are using ASP.NET MVC 3 and Razor. In this version jQuery is the default client scripting framework. No more MicrosoftAjax.js (thanks God). So:

function onComplete_AdminReviewOption(ajaxContext) {
    var jsonObject = eval(ajaxContext);
}

Also don't forget to include jquery.unobtrusive-ajax.js.

If you want to use the legacy stuff you could by setting the following in your web.config:

<add key="UnobtrusiveJavaScriptEnabled" value="false" />

By default this variable is set to true.

Personally I prefer to use standard links:

@Html.ActionLink(
    Model.IsVisible ? "Disable" : "Enable",  // <-- this should probably be as a property directly in the view model, ex. Model.LinkText
    "ToggleVisibility", 
    "Review", 
    new { area = "Admin", id = Model.Id }, 
    new { id = "myLink" }
)

and AJAXify them using jQuery in a separate javascript file:

$(function() {
    $('#myLink').click(function() {
        $.post(this.href, function(result) {
            // result is already a javascript object, no need to eval
        });
        return false;
    });
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文