title: Problem with Session Variable value in ASP.NET Core Project. Takes the value on second time the page Loads
body: <p>I have a project in which I have some Records from database in a table. So it displays 10 out of all in each page.</p>
  <p>The User check some rows(with checkBox) and I keep those rows Id's and send them with Ajax in GetCheckedList Method when the user push Next Page Button. This methods makes the values a string and Store them as Session Variable.</p>
  <p>The problem is that when Index takes Session Value, after GetCheckedList execution the very first time the value is Null but when User navigates for another time in Next or Previous page the Session Value on index is Correct.</p>
  <p>Something I lose here with which method runs first but I cant figure it out and I need some help.</p>
  <p>Index</p>
  <pre><code>public  async Task&lt;IActionResult&gt; Index(...)
  {
     int pageSize = 10;
     var users = from a in _context.Users
                             select a;
   
    ...
    
    //Session variable 
    var checkedList = HttpContext.Session.GetString(&quot;CheckedSession&quot;);
              if (checkedList != null)
              {
                      ViewBag.checkedListVB = checkedList;
                       HttpContext.Session.SetString(&quot;CheckedSession1&quot;, checkedList);
              }
  
    return View(await App.PaginatedList&lt;User&gt;.CreateAsync(users.AsNoTracking(), pageNumber ?? 1, pageSize));
  }
  
  </code></pre>
  <p>GetCheckedList</p>
  <pre><code>public RedirectResult GetCheckedList(List&lt;string&gt; values, string text)
  {  
     List&lt;string&gt; str = values;
     str.RemoveAll(item =&gt; item == null);
     List&lt;int&gt; intList = str.ConvertAll(int.Parse);
  
     var finalCheckList = string.Join(&quot;,&quot;, intList);
  
  
                  if (HttpContext.Session.GetString(&quot;CheckedSession&quot;) != null)
                  {
                      var temp = HttpContext.Session.GetString(&quot;CheckedSession&quot;);
                      string[] tempStringList = temp.Split(&quot;,&quot;);
                      var removeEmptytemp = tempStringList.Where(o =&gt; o != &quot;&quot;).ToArray();
                      int[] myIntsTemp = Array.ConvertAll(removeEmptytemp, int.Parse);
                      List&lt;int&gt; myIntsTempList = myIntsTemp.ToList();
  
                      List&lt;int&gt; ulist = myIntsTempList.Union(intList).ToList();
  
                      var now = string.Join(&quot;,&quot;, ulist);
                      finalCheckList = string.Join(&quot;,&quot;,now);
                  }
  
     HttpContext.Session.SetString(&quot;CheckedSession&quot;, finalCheckList);
  
     return new RedirectResult(url: &quot;/Users/Index&quot;, permanent: false,
                               preserveMethod: true);
  }
  </code></pre>