简体   繁体   中英

ASP.net MVC unable to get value back from Controller

in my ASP.net MVC application, I have a button that call a controller action which takes longer (>60 mins) to complete. To keep my session alive I am using setInterval function.

reference: http://www.dotnetcurry.com/ShowArticle.aspx?ID=453&AspxAutoDetectCookieSupport=1

view looks like:

@Html.DevExpress().Button(settings =>
                        {
                            settings.Name = "LoadData";
                            settings.Text = "Load Data";
                            settings.ClientSideEvents.Click = string.Format("function(s, e) {{ OnButtonClick(s, e, '{0}', '{1}'); }}", Url.Action("Start", "ImportData", null), Url.Action("alive", "ImportData", null));
                            settings.UseSubmitBehavior = false;
                        }).GetHtml()

my OnButtonClick Function looks:

function OnButtonClick(s, e, startUrl, progressUrl) {
    //debugger;
    StartActionOnServer(startUrl);
    setInterval(keepalive, 600000); //every 10 minutes. 
}

Keep Alive looks like:

function keepalive()
{
    console.log("I am alive");
    $.ajax({
        type: 'POST',
        url: '/ImportData/alive',
        dataType: "text",
        dataType: 'json',
        contentType: "application/json; charset=utf-8",
        success: function (msg) {
            debugger;
            console.log("pinging");
        },
        Error: function (xhr) {
            debugger;
            alert(xhr)
        },
        });
 }

my issue is that code is not hitting my Controller function, as result I never get result back in my success block.

success: function (msg) {
            debugger;
            console.log("pinging");
        },

Controller action function:

[HttpPost]
    public ActionResult alive()
    {
        var result = "Still Alive";
        return Content(result);
    }

Instead of me hardcoding: console.log("I am alive"); I would like Controller to return this.

my console.log looks like attached screeshot

在此处输入图片说明

Any idea how to get value from Controller ?What am I doing wrong here. Thanks.

This solved my issue:

function keepalive()
{
    console.log("I am alive");
    $.ajax({
        type: 'POST',
        url: '/ImportData/alive',
        dataType: "text",
        contentType: "text",
        success: function (msg) {
            console.log(msg);
        },
        Error: function (xhr) {
            alert(xhr)
        },
    });
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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