views:

599

answers:

1

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.

ORIGINAL QUESTION:

I'm having a weird issue and I'm hoping someone can help shed some light on the situation.

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.

Here's where the issue comes in:

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.

Here's how I can tell there is a problem:

  1. I load the page initially, mock cart saved to session.
  2. Reload the page to check that the cart was saved to session correctly.
  3. Type in the promo code, break point on the cart retrieval process shows no cart in session.
  4. Mock cart again saved to session, this time with the discount applied.
  5. Reload the page again, cart retrieved from session, but it's the first cart I saved.
  6. Re-enter the promo code, this time it does find a cart in session, but it's the one with the discount already applied.
  7. I then repeat this process several times to double check my sanity, but what I described is very much happenning as I described it.

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.

EDIT: Per request below, here is the code for saving/retrieving the cart:

private void SaveCart(ShoppingCartContainer cart)
{
    Session["Cart"] = cart;
}

private ShoppingCartContainer RetrieveCart()
{
    ShoppingCartContainer cart = (ShoppingCartContainer)Session["Cart"];
    if (cart != null)
    {
        return cart;
    }
    return null;
}

EDIT: Here are the action methods

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" });
}

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.

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.

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:

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?

+1  A: 

Are you sure you use the same cart session variable in the both actions?

Gregoire
I'm not that familiar with session in ASP.NET outside of adding and removing items. I did, however, check the session id on both sessions and it's consistently been the same for both every time.
dhulk
what is your code to retrieve and save you cart in your both actions?
Gregoire
Code for cart saving/retrieval added to question
dhulk
Except that you can simplify : "if (cart != null) { return cart; } return null;"in "return cart;" I see nothing wrong in this code. can we see your both action codes?
Gregoire
Action code added
dhulk
Sorry I haven't got any clue for this. It do not see anything wrong
Gregoire
Yeah I can't see anything wrong either and neither can anybody else. I've never heard of AJAX interfering with session or causing it to behave in an unexpected manner. There is some more info above if you care to look, but I'm going to move on to a different portion of the project for now. Thanks for taking the time to look into it.
dhulk