title: Why is my Session["value"] not persisting across views (asp.net mvc)
body: <p>I am electing to do session to store a value that I will need to call and update throughout a handful of controllers and views. I know it is possible to do something like this with a BaseViewModel.cs, or something less <em>session-y</em>, but I am trying to see how Session can possibly solve my needs. That out of the way, here is what I am doing:</p>
  <h1>Flow</h1>
  <p>I have a partial view that is rendered on my _layout page like so:</p>
  <pre><code>@Html.Action(&quot;OrgSwitch&quot;, new { controller = &quot;Common&quot;, area = &quot;InkScroll&quot; })
  </code></pre>
  <p>This is a drop down list containing a logged in users organizations. It hits a CommonController that takes care of things like rendering model-bound logic on layout pages. In the CommonController I have this view and a postback, like so:</p>
  <pre><code>[ChildActionOnly]
      public ViewResult OrgSwitch()
      {
          var userOrgs = new List&lt;SelectListItem&gt;();
          var user = Ctx.Users.FirstOrDefault(x =&gt; x.UserName == User.Identity.Name);
          int key = 0;
          if (user.Organizations.Count &gt; 0)
          {
              TempData[&quot;HasOrgs&quot;] = true;
              foreach (var org in user.Organizations)
              {
                  if (Session[&quot;SelectedOrgKey&quot;] == null)
                  {
                      //currently setting selected by primary org id
                      //todo: set selected to tempdata selectedOrgId if there is one.
                      userOrgs.Add(org.OrganizationId.ToString() == user.PrimaryOrgId
                          ? new SelectListItem { Text = org.Name, Value = org.OrganizationId.ToString(), Selected = true }
                          : new SelectListItem { Text = org.Name, Value = org.OrganizationId.ToString(), Selected = false });
                  }
                  else
                  {
                      key = Convert.ToInt32(Session[&quot;SelectedOrgKey&quot;]);
                      userOrgs.Add(org.OrganizationId == key
                          ? new SelectListItem { Text = org.Name, Value = org.OrganizationId.ToString(), Selected = true }
                          : new SelectListItem { Text = org.Name, Value = org.OrganizationId.ToString(), Selected = false });
                  }
              }
  
              ViewBag.UserOrgs = userOrgs.OrderBy(x =&gt; x.Text);
          }
          else
          {
              ViewBag.UserOrgs = userOrgs;
              TempData[&quot;HasOrgs&quot;] = false;
          }
          Session[&quot;SelectedOrgKey&quot;] = key;
          return View();
      }
  
      [HttpPost]
      public RedirectToRouteResult OrgSwitch(string UserOrgs)
      {
          Session[&quot;SelectedOrgKey&quot;] = UserOrgs;
          return RedirectToAction(&quot;Index&quot;, &quot;Recruiter&quot;, new { orgId = UserOrgs, area = &quot;InkScroll&quot; });
      }
  </code></pre>
  <p>This more or less on initial load will render the drop down list and default it to the users primary org. If they select something and post back that selection, it will display the selected one by default on subsequent pages.</p>
  <h1>attempt and failure</h1>
  <p>I am trying various ways of utilizing the Session value. One area in a viewresult:</p>
  <pre><code> [ChildActionOnly]
      public ViewResult StaffList()
      {
          var user = Ctx.Users.FirstOrDefault(x =&gt; x.UserName == User.Identity.Name);
  
          var model = new List&lt;StaffListViewModel&gt;();
          int key = Convert.ToInt32(Session[&quot;SelectedOrgKey&quot;]);
          var org = user.Organizations.FirstOrDefault(x =&gt; x.OrganizationId == key);
          if (org.RegisteredMembers != null)
          {
              foreach (var req in org.RegisteredMembers)
              {
                  model.Add(new StaffListViewModel
                  {
                      UserName = req.UserName,
                      Name = (req.FirstName ?? &quot;&quot;) + &quot; &quot; + (req.LastName ?? &quot;&quot;),
                      ImageLocation = req.ImageLocation
                  });
              }
          }
          Session[&quot;SelectedOrgKey&quot;] = key;
          return View(model);
      }
  </code></pre>
  <p>in here 'key' is coming up empty.</p>
  <p>Another try is putting it in the layout cshtml file like so:</p>
  <pre><code>&lt;li&gt;&lt;a href=&quot;@Url.Action(&quot;Staff&quot;, &quot;Recruiter&quot;, new {area=&quot;&quot;, 
                  orgId = Convert.ToInt32(Session[&quot;SelectedOrgId&quot;])})&quot;&gt;Staff&lt;/a&gt;&lt;/li&gt;
  </code></pre>
  <p>in this one if I hover over the link, the orgId is always equal to 0. Am I messing this up somewhere? Really, I need to have this SelectedOrgId available to any page on the application.</p>