简体   繁体   English

如何在MVC中从jquery调用控制器?

[英]How to call controller from jquery in MVC?

I want to call my controller from view by Jquery which returns count. 我想通过返回数量的Jquery从视图中调用我的控制器。 While debugging the result variable returns as undefined. 在调试时,结果变量将返回未定义状态。 Please someone correct my code. 请有人更正我的代码。

View: 视图:

<script type="text/javascript">
$(function () {
    $('.icon-delete').click(function () {
        debugger;
        var v = 'e8a6bdf4-1da6-41e0-8423-86ffacf71703';
       var result= GetCollectionCount(v);
        var answer = confirm('Do you want to delete this record?');
        if (answer) {
            $.post(this.href, function () {
                window.location.reload(); //Callback
            });
            return false;
        }
        return false;
    });
});

function GetCollectionCount(id) {
    $.ajax({
        type: 'POST',
        contentType: "application/json;charset=utf-8",
        url: '/AdminNew/CollectionCount',
        dataType: 'json',
        data: { "id": id },
        //traditional: true,
        success: function (ajaxresult) {
            //alert(ajaxresult);
            return ajaxresult;
        },
        failure: function (ajaxresult, status) {
            console.log(ajaxresult)
        }
    });
}

While debugging the result variable returns as undefined. 在调试时,结果变量将返回未定义状态。

That's normal and is how AJAX works. 这是正常现象,也是AJAX的工作方式。 The first A in AJAX stands for Asynchronous . AJAX中的第一个A代表异步 This means that the $.ajax function doesn't return anything. 这意味着$.ajax函数不会返回任何内容。 It is used to trigger an AJAX request to the server and the only single place where you can use the results of this AJAX call is inside the success callback. 它用于触发对服务器的AJAX请求,并且可以使用此AJAX调用的结果的唯一单个位置success回调内部。 In your example you seem to be attempting to use those results immediately. 在您的示例中,您似乎正在尝试立即使用这些结果。

The problem is GetCollectionCount is making an Asynchronous request to the server. 问题是GetCollectionCount正在向服务器发出异步请求。 That is to say the rest of your code continues to execute and does not wait for the request to finish. 也就是说,其余代码将继续执行,而不会等待请求完成。

You need to place the code dependant on the request in a callback that is executed upon a successful request: 您需要将依赖于请求的代码放置在成功请求后执行的回调中:

$(function () {
    $('.icon-delete').click(function () {
        debugger;
        var v = 'e8a6bdf4-1da6-41e0-8423-86ffacf71703';
        GetCollectionCount(v);
    });
});

function GetCollectionCount(id) {
    $.ajax({
        type: 'POST',
        contentType: "application/json;charset=utf-8",
        url: '/AdminNew/CollectionCount',
        dataType: 'json',
        data: { "id": id },
        //traditional: true,
        success: function (ajaxresult) {
            var answer = confirm('Do you want to delete this record?');
            if (answer) {
              $.post(this.href, function () {
                window.location.reload(); //Callback
              });
              return false;
            }
            return false;
        },
        error: function (ajaxresult, status) {
            console.log(ajaxresult)
        }
    });
}

You should try something like that : 您应该尝试这样的事情:

$(function () {
    $('.icon-delete').click(function () {
        debugger;
        var v = 'e8a6bdf4-1da6-41e0-8423-86ffacf71703';
        GetCollectionCount(v);
    });
});

function GetCollectionCount(id) {
    $.ajax({
        type: 'POST',
        contentType: "application/json;charset=utf-8",
        url: '/AdminNew/CollectionCount',
        dataType: 'json',
        data: { "id": id },
        //traditional: true,
        success: getCollectionSuccess,
        error: errorCallback
    });
}

function getCollectionSuccess(ajaxResult){
    var answer = confirm('Do you want to delete this record?');
        if (answer) {
            $.post(this.href, function () {
                window.location.reload(); //Callback
            });
            return false;
       }
}

function errorCallback(errorMsg, status){
    console.log("Error Message : "+errorMsg+" Error code : "+status);
}

Maybe setting async to false gets away the problem @Darin mentioned: 也许将async设置为false可以解决@Darin提到的问题:

function GetCollectionCount(id) {
$.ajax({
    // ... your settings

    async: false,

    success: function (ajaxresult) {
        //alert(ajaxresult);
        return ajaxresult;
    },
    failure: function (ajaxresult, status) {
        console.log(ajaxresult)
    }
});

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM