Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I pass an argument to the Success Function of a Ajax.ActionLink

I've got an Ajax.ActionLink like so:

@Ajax.ActionLink("Load Related Titles",
"AjaxLoadRelated",
"Shared",
new { RelatedComics = c.RelatedComic.RelatedTitles },
new AjaxOptions() { HttpMethod = "Get", UpdateTargetId = "divRelatedTitles", LoadingElementId = "divLoading", OnBegin = "ajaxLoadMoreBegin", OnFailure = "ajaxLoadMoreFailed", OnSuccess = "ajaxLoadMoreSuccess" })

Currently my OnSuccess function looks like so:

function ajaxLoadMoreSuccess() {
    ...
}

I would like to pass an argument to it:

function ajaxLoadMoreSuccess(myArg) {
    ...
}

Is this possible?

Thanks in advance

like image 386
Sniffer Avatar asked Oct 14 '11 19:10

Sniffer


1 Answers

It's possible to do. The mvc ajax unobtrusive script creates an anonymous function with three parameters(corresponding to the parameters for the jquery ajax complete method) data, status, xhr and calling that method with the arguments for the jquery ajax complete method. So for example:

...OnSuccess = "ajaxLoadMoreSuccess1('hello world')"

Or

...OnSuccess = "ajaxLoadMoreSuccess2(calc(), data, status, xhr)"

<script type="text/javascript">
    function ajaxLoadMoreSuccess1(message) {
        alert(message);
    }

    function ajaxLoadMoreSuccess2(x, data, status, xhr) {
        $('#somespan').text('calc was ' + x + ' and data is ' + data);
    }

    function calc() {
        return 1 + 2;
    }
</script>
like image 55
Sperling Avatar answered Nov 08 '22 23:11

Sperling