locked
Increment Session Variable Every Second Using A For Loop In The Controller In ASP NET Core RRS feed

  • Question

  • User1596943402 posted

    I would like to write a for loop in the controller that increments a session variable every second in ASP Net Core. I would like to only do the for loop in the controller, and display the data in the view.

    However, when I write a for loop, the session variable only displays one result, and does not increment.

    Ideally, I would like to mimick this example here JS Fiddle Increment Example but it is written in Javascript and jQuery:

    $(document).ready(function () {
    var number = parseInt($('#test').text(), 10) || 0; // Get the number from paragraph
    
    
    // Called the function in each second
    var interval = setInterval(function () {
        $('#test').text(number++); // Update the value in paragraph
    
        if (number > 1000) {
            clearInterval(interval); // If exceeded 100, clear interval
        }
    }, 1000); // Run for each second
    });
    Sunday, August 16, 2020 1:17 AM

All replies

  • User-474980206 posted

    Your sample code is updating the dom in the browser. Session is on the server. You need to do an Ajax call to update session. 

    on the server, session is only available  during a request. If you did a for loop in the controller, updating session, the response would never return to the client. 

    Sunday, August 16, 2020 4:15 PM
  • User-17257777 posted

    Hi Lisa,

    It is not reasonable change the value of seesion cyclically in one request, this will block the current request.

    I think you can send a request every one second from the client side, then updatet session in each request.

    Best Regards,

    Jiadong Meng

    Tuesday, August 18, 2020 7:27 AM
  • User1596943402 posted

    Thank you Jiadong and Bruce.  Since the only option is an ajax call, can either of you point me towards a good video tutorial, or article that shows how this may be accomplished?  Thank you.

    Wednesday, August 19, 2020 8:53 PM
  • User-17257777 posted

    Hi Lisa,

    Just Use the sample code

    <label>Time Now:</label>
    <span id="sessionValue">
    
    </span>
    
    @section scripts{
        <script>
        $(document).ready(function () {
            var interval = setInterval(function () {
                // call backend to update the value in session
                $.ajax({
                    type: 'post',
                    url: 'Home/UpdateSession',
                    success: function (result) {
                        $("#sessionValue").text(result.data);
                    }
                })
                
            }, 1000); // Run for each second
        });
        </script>
    }

    Controller:

    public IActionResult Index()
    {
        HttpContext.Session.SetString("Time", DateTime.Now.ToString());
        return View();
    }
    
    [HttpPost]
    public IActionResult UpdateSession()
    {
        HttpContext.Session.SetString("Time", DateTime.Now.ToString());
        return Json(new { data = HttpContext.Session.GetString("Time") });
    }

    Result:

    Best Regards,

    Jiadong Meng

    Thursday, August 20, 2020 9:14 AM
  • User1596943402 posted

    Hello Jiadong, 

    I used the sample code and it works great!

    I am amazed at your skill! 

    Thank you! 

    Friday, August 21, 2020 2:06 PM