I have a pretty simple controller:
public class HomeController : Controller
{
public ActionResult Index()
{
Session["SomeData"] = "123";
return View();
}
[HttpPost]
public ActionResult LongTest()
{
Thread.Sleep(5000);
return Json(new { Text = DateTime.Now.ToString("hh:mm:ss.fff") + " - LongTest"});
}
[HttpPost]
public ActionResult CantAnswer()
{
return Json(new { Text = DateTime.Now.ToString("hh:mm:ss.fff") + " - CantAnswer"});
}
}
I use these methods from the client's side this way:
<script type="text/javascript">
$(document).ready(function () {
$('#btnLongOperation').click(function () {
$.post("/Home/LongTest", null, function (data) {
$('#result').text(data.Text);
}, "json");
});
$('#btnWnotWork').click(function () {
$.post("/Home/CantAnswer", null, function (data) {
$('#result').text(data.Text);
}, "json");
});
});
</script>
<div>
<input id="btnLongOperation" type="button" value="Long operation"/>
<input id="btnWnotWork" type="button" value="Won't work"/>
</div>
<div id="result">
If I click the first button and then without waiting for 5 seconds click the second button my second action won't be called. If you remove the string with using session in the Init method you will see that the actions are able to be called without waiting for each other. However, once you use session object you will not see a result of second action untill the first one is finished. Can anyone explain this behavior of asp.net mvc?