title: ASP.NET MVC Session using jQuery
body: <p>PROBLEM SOLVED: 
  I was overlooking a very simple issue, and that is that this is actually two different apps running under one website. I modified the URL in the AJAX call slightly to reflect that and the problem is no longer happening.</p>
  
  <p>ORIGINAL QUESTION:</p>
  
  <p>I'm having a weird issue and I'm hoping someone can help shed some light on the situation.</p>
  
  <p>I have a page, your standard issue shopping cart where I'm trying to get a promo code submission to work via AJAX. Everything is working out fine except for my session state. When I first load up the shopping cart page, I look for a cart in session (if it isn't there I mock one since I'm merely templating/testing things right now) and right before I return the view I save the cart back into session. I do the same thing in my AJAX action that returns a partial view.</p>
  
  <p>Here's where the issue comes in:</p>
  
  <p>It seems as if two different versions of the cart are coming back from session depending on which action I'm going to, which of course is not what I'm after.</p>
  
  <p>Here's how I can tell there is a problem:</p>
  
  <ol>
  <li>I load the page initially, mock cart saved to session.</li>
  <li>Reload the page to check that the cart was saved to session correctly.</li>
  <li>Type in the promo code, break point on the cart retrieval process shows no cart in session.</li>
  <li>Mock cart again saved to session, this time with the discount applied.</li>
  <li>Reload the page again, cart retrieved from session, but it's the first cart I saved.</li>
  <li>Re-enter the promo code, this time it does find a cart in session, but it's the one with the discount already applied.</li>
  <li>I then repeat this process several times to double check my sanity, but what I described is very much happenning as I described it.</li>
  </ol>
  
  <p>So now I am very confused and also frustrated since this is the only thing in my way at the moment. I'm hoping that it's something simple that I'm missing, but searching for this issue (here and Google) is giving me results covering a very broad spectrum of topics.</p>
  
  <p>EDIT: Per request below, here is the code for saving/retrieving the cart:</p>
  
  <pre><code>private void SaveCart(ShoppingCartContainer cart)
  {
      Session["Cart"] = cart;
  }
  
  private ShoppingCartContainer RetrieveCart()
  {
      ShoppingCartContainer cart = (ShoppingCartContainer)Session["Cart"];
      if (cart != null)
      {
          return cart;
      }
      return null;
  }
  </code></pre>
  
  <p>EDIT: Here are the action methods</p>
  
  <pre><code>public ActionResult ListItems(string userid)
  {
      var retval = RetriveCart();
      if( retval == null)
      {
          retval = _model.Show(userid);
          if (retval == null)
          {
              return Redirect("/");
          }
      }
      SaveCart(retval);
      return View("List", retval);
  }
  
  public ActionResult ApplyPromoCode(string promocode, string userid)
  {
      var cart = RetriveCart();
      if (cart == null)
      {
          cart = _model.Show("blah");
      }
      cart = _model.ApplyPromoCode(promocode, "blah");
      SaveCart(cart);
      if (Request.IsAjaxRequest())
      {
          return PartialView("ShoppingCartFooter", cart);
      }
      return RedirectToAction("ListItems", new { userid = "blah" });
  }
  </code></pre>
  
  <p>NOTE: As I said, I'm doing templating/testing so some of the code between the Retrieve and Save calls may be a little nonsensical. I have more or less ruled those out as the issues however, as the problem happens before I even get to those.</p>
  
  <p>UPDATE:
  Further confirming my suspicions about what's going on is that when I simply submit the form without jQuery catching the form submission, the cart gets saved properly, so that when I load the page normally, the promo code remains applied.</p>
  
  <p>UPDATE #2:
  Just looked into what was happening on the first AJAX call. It appears that the session variable has the "IsNewSession" property set to new on the first AJAX call, even though I've already started on upon arrival to the page. I can't explain why this is happenning, but here's some more info that might be relevant:</p>
  
  <p>We are using the Windsor controller factory (by "we", I don't mean me, I'm just hooking up templates to views and doing enough back-end code to get the flow working properly) I'm unfamiliar with it, but could that be part of the problem?</p>