使用 jQuery 的 ASP.NET MVC 会话

发布于 2024-08-04 02:43:23 字数 2274 浏览 7 评论 0原文

问题已解决: 我忽略了一个非常简单的问题,那就是这实际上是在一个网站下运行的两个不同的应用程序。我稍微修改了 AJAX 调用中的 URL 以反映这一点,并且问题不再发生。

原始问题:

我遇到了一个奇怪的问题,我希望有人可以帮助阐明这种情况。

我有一个页面,您的标准问题购物车,我正在尝试通过 AJAX 提交促销代码。除了我的会话状态之外,一切都很顺利。当我第一次加载购物车页面时,我会在会话中查找购物车(如果不存在,我会模拟一个购物车,因为我现在只是在模板化/测试东西),然后在返回视图之前保存购物车返回会话。我在返回部分视图的 AJAX 操作中执行相同的操作。

这就是问题所在:

似乎有两个不同版本的购物车从会话中返回,具体取决于我要执行的操作,这当然不是我想要的。

以下是我如何判断存在问题的方法:

  1. 我最初加载页面,模拟购物车保存到会话中。
  2. 重新加载页面以检查购物车是否已正确保存到会话中。
  3. 输入促销代码,购物车检索过程中的断点显示会话中没有购物车。
  4. 模拟购物车再次保存到会话中,这次应用了折扣。
  5. 再次重新加载页面,从会话中检索购物车,但这是我保存的第一个购物车。
  6. 重新输入促销代码,这次确实在会话中找到了购物车,但它是已应用折扣的购物车。
  7. 然后,我多次重复这个过程,以再次检查我的理智,但我所描述的情况与我所描述的情况完全一样。

所以现在我很困惑也很沮丧,因为这是我目前唯一的障碍。我希望这是我所缺少的一些简单的东西,但是搜索这个问题(在这里和谷歌)给了我涵盖非常广泛的主题的结果。

编辑:根据下面的请求,这里是保存/检索购物车的代码:

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

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

编辑:这里是操作方法

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

注意:正如我所说,我正在做模板/测试,因此检索和保存调用之间的一些代码可能是有点无稽之谈。然而,我或多或少地排除了这些问题,因为问题在我解决这些问题之前就发生了。

更新: 进一步证实我对正在发生的事情的怀疑是,当我简单地提交表单而没有 jQuery 捕获表单提交时,购物车会正确保存,这样当我正常加载页面时,促销代码仍然适用。

更新#2: 刚刚查看了第一个 AJAX 调用时发生的情况。看起来会话变量在第一次 AJAX 调用时将“IsNewSession”属性设置为 new,即使我在到达该页面时就已经开始了。我无法解释为什么会发生这种情况,但这里有一些可能相关的更多信息:

我们正在使用 Windsor 控制器工厂(“我们”,我不是指我,我只是将模板连接到视图和做足够的后端代码以使流程正常工作)我对此不熟悉,但这可能是问题的一部分吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜灵血窟げ 2024-08-11 02:43:23

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文