ASP.NET MVC session using jQuery
PROBLEM SOLUTION: I didn’t notice a very simple problem, and it is that there are actually two different applications running under the same website. I slightly changed the url in the AJAX call to reflect this and the problem no longer happens.
ORIGINAL QUESTION:
I have a strange problem and I hope someone can help shed some light on the situation.
I have a page, your standard shopping cart, where I am trying to get a promo code to work via AJAX. Everything works fine except for my session state. When I first load the shopping cart page, I look at the cart in the session (if not, I mock it as I am just templating / checking things right now) and right before I go back, I save the cart to go back to the session ... I am doing the same in my AJAX action which returns a partial view.
Here's the problem:
It seems that two different versions of the cart are being returned from the session, depending on what action I am going to do, which of course is not what I want.
This is how I can tell there is a problem:
- First, I load the page by unloading the cart into the session.
- Load the page to check if the telegram was saved correctly in the session.
- Enter the promo code, the breakpoint in the basket search process does not show the telegram in the session.
- The mock cart is saved per session again, this time with the discount applied.
- Reload the page again, the cart is retrieved from the session, but this is the first cart I saved.
- Re-enter the promo code, this time it will find the cart in the session, but the one who already has the discount.
- Then I repeat this process several times to double check my sanity, but what I have described happens very often when I describe it.
So now I am very confused and frustrated as this is the only thing on my way at the moment. I hope this is something simple that I am missing, but searching for this problem (here and Google) gives me results covering a very wide range of topics.
EDIT: For the request, below is the code to save / retrieve 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: Following 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 Get and Store calls might be a little pointless. I have more or less posed this as a problem, however, as the problem occurs before I even get to them.
UPDATE: Further confirmation of my suspicions of what's going on is that when I just submit the form without jQuery, hijacking the submit form, the cart will be saved properly so that when I load the page normally the promo code stays applied.
UPDATE # 2: Just looked at what happens on the first AJAX call. It looks like the session variable has the "IsNewSession" property set in a new one on the first AJAX call, although I already got started when I arrived at the page. I cannot explain why this is happening, but there is additional information here that may be relevant:
We are using the Windsor factory controller ("we" I don't mean me, I just hook the templates to the views and do enough code in the background to get the thread to work as expected) m unfamiliar with it, but could this be part of Problems?
source to share