2017-04-24 3 views
0

Ich kann die Sitzungswerte nicht abrufen, wenn ich eine andere Operation starte, während eine Operation ausgeführt wird, wie unten;MVC Parallele Transaktionen und Sitzungsobjekte

$.ajax({ 
     type: 'GET', 
     cache: false, 
     url: '/Home/StartOperation', 
     dataType: 'json', 
     data: { customerId: $("#txtCustomerId").val() }, 
     error: function (xhr, status, error) { 
     $("#textlabel").text(error) 
     }, 
     success: function (result) { 
     $("#textlabel").text(result.Result) 
     } 
    }); 

Steuerungsseite;

public ActionResult StartOperation(string customerId) 
    { 
     Session["OperationId"] = "operationid"; 
     // Some web api transactions (This operations takes a few minutes.) 

     var data = new { Result = "Operation Completed." }; 
     return Json(data, JsonRequestBehavior.AllowGet); 
    } 

Ich bin sicher, über Session["OperationId"] ist nicht null, und dann meine Abbrechen Ich nenne Aktion während Web-api Transaktionen im Gange;

$(function() { 
     $('#btnCancelLogin').on('click', function() { 
      $.ajax({ 
       type: 'GET', 
       cache: false, 
       url: '/Home/CancelOperation' 
      }); 
     }); 
    }); 

Steuerungsseite;

public ActionResult CancelOperation() 
{ 
    String operationId = Session["OperationId"] as String // return null 
    //Cancel operations 
} 

Warum Session["OperationId"] auf CancelOperation() Methode immer null ist? Danke für Ratschläge.

+0

Try sein: '(String) Session [" OperationId "]' – Sajal

+0

@SajalS, hat nicht funktioniert, Außerdem habe ich versucht, ohne zu werfen, aber Session ["OperationId"] war null. – oyenigun

+0

Können Sie die Browser-Konsole öffnen und prüfen, ob Session [Key] während der StartOperation-Methode tatsächlich erstellt wird? – Sajal

Antwort

0

Wenn Sie Daten von AJAX-Seite für den Startvorgang nehmen, sollten Sie Post verwenden. Eigentlich ist dein Ajax-Typ falsch.

$.ajax({ 
     type: 'POST', 
     cache: false, 
     url: '/Home/StartOperation', 
     dataType: 'json', 
     data: { customerId: $("#txtCustomerId").val() }, 
     error: function (xhr, status, error) { 
     $("#textlabel").text(error) 
     }, 
     success: function (result) { 
     $("#textlabel").text(result.Result) 
     } 
    }); 

Oder wenn Sie von der Steuerung zu übernehmen Daten möchten, sollten Sie geben Daten

public ActionResult StartOperation(string customerId) 
    { 
     Session["OperationId"] = "operationid"; 
     // Some web api transactions 
     return json(customerId); 
    } 

auch sollten Sie Ihre Ajax ähnliche

$.ajax({ 
     type: 'GET', 
     cache: false, 
     url: '/Home/StartOperation', 
     dataType: 'json', 
     data: { customerId: $("#txtCustomerId").val() }, 
     error: function (xhr, status, error) { 
     $("#textlabel").text(error) 
     }, 
     success: function (result) { 
     $("#textlabel").text(result.Result) 
     } 
    }); 
+0

Ich habe bereits json Daten zurückgegeben nach einigen Web-API-Transaktionen, bearbeite ich meinen Beitrag. – oyenigun