문제

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?

도움이 되었습니까?

해결책

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;
    });
});
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top