开发者

Problem with Ajax.ActionLink AjaxOptions Callbacks (ASP.NET MVC 3)

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开发者_如何学Go 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?


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜