简体   繁体   English

使用jQuery的ASP.NET MVC会话

[英]ASP.NET MVC Session using jQuery

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. 我稍微修改了AJAX调用中的URL以反映这一点,该问题不再发生。

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. 我有一个页面,您的标准问题购物车,我正在其中尝试通过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. 我在返回部分视图的AJAX操作中执行相同的操作。

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. 我希望这很简单,但是我一直在寻找这个问题(这里和Google),结果涵盖了非常广泛的主题。

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. 注意:正如我所说,我正在做模板/测试,因此Retrieve和Save调用之间的某些代码可能有点荒谬。 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. 更新:进一步证实了我对正在发生的事情的怀疑是,当我只提交表单而没有jQuery捕获表单提交时,购物车被正确保存,因此当我正常加载页面时,促销代码仍然适用。

UPDATE #2: Just looked into what was happening on the first AJAX call. 更新#2:只是调查了第一次AJAX调用中发生的情况。 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. 似乎在第一个AJAX调用中,会话变量的“ IsNewSession”属性设置为new,即使我已经在到达页面时就开始了。 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? 我们正在使用Windsor控制器工厂(按“我们”的意思,我并不是说我,我只是将模板连接到视图并执行足够的后端代码以使流程正常工作),我对此并不熟悉,但这可能是问题的一部分吗?

您确定在两个操作中都使用相同的购物车会话变量吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM