【问题标题】:Problem with View - it does not refresh after db update视图问题 - 数据库更新后它不会刷新
【发布时间】:2010-05-13 09:30:15
【问题描述】:

我正在使用小型 ASP.NET MVC 项目 - 在线商店。

我有 addToCart 方法,可以将选定的产品添加到购物车 - 它更新我的数据库中的购物车表并显示购物车视图及其内容。但我有问题。虽然 db 正确更新,但视图没有正确更新。我看到我的数据库中的产品数量正确增加,但视图中的数量没有改变。我必须停止在 Visual studia 中调试我的应用程序并重新启动它 - 然后我的视图显示正确的数据。有什么问题?

我正在使用 LINQ to Entity。从购物车存储库添加方法:

public void Add(int product, int quantity, string user)
{
    Cart cart = null;
    cart = (from c in de.Cart
            where (c.userName == "testUser" && c.productId == product)
            select c).First();
    // query is searching for existing product of testUser and id specified in parameter in cart and get it

    cart.quantity += 1; //increment quantity

    de.SaveChanges();   // save entity
}

来自控制器的 AddToCart 方法:

public void AddToCart(int pid, int quant, string usr)
{
    _cartRep.Add(pid,quant,usr);
}

返回购物车视图的方法:

public ActionResult Cart()
{
    IEnumerable<CartInfo> model = _cartRep.GetTrans();
    return View(model);
}

这里是 GetTrans() 实现:

    public IEnumerable<CartInfo> GetTrans()
    {
        using (DBEntities de = new DBEntities())
        {

            return (from c in de.Cart
                    where (c.userName == "testUser")
                    select new CartInfo
                               {
                                   Id = c.id,
                                   ProductId = c.productId,
                                   Quntity = c.quantity,
                                   Realized = c.realized,
                                   UserName = c.userName,
                                   Value = c.value,
                                   Products = (from p in de.Product
                                               where (p.id == c.productId)
                                               select new ProductInfo
                                                          {
                                                              Category = p.Category,
                                                              Desc = p.Description,
                                                              Id = p.id,
                                                              Image = p.Image,
                                                              Name = p.Name,
                                                              Quntity = p.Quantity,
                                                              Price = p.Price
                                                          })
                               }).ToList();
        }
    }

如您所见,我的用户名是硬编码的。我这样做只是为了测试。如果我知道它正在工作,我会改进代码。感谢 .FristOrDefault() 的好建议

【问题讨论】:

  • 我们是否有机会从购物车存储库中看到 GetTrans() 的代码?我目前可以看到您的代码中有很多潜在问题,例如:您将一定数量的产品传递给 Add(pid,quant,usr) 然后您将用户名和数量硬编码到搜索和添加;如果用户的购物车中没有该产品,“.First()”会抛出异常(如果找不到,.FirstOrDefault() 会给你一个空值)等等 - 我会如果您此时返回错误的用户购物车,请不要感到惊讶。

标签: c# asp.net-mvc model-view-controller


【解决方案1】:

如果您在表单发布后返回相同的视图,Html 助手会从模型状态而不是模型中获取数据。 要在视图中获取更新的数据,请使用 post redirect get 模式或 ModelState.Clear()

【讨论】:

  • 哇。世界又开始变得有意义了。这让我今天很兴奋。谢谢。
【解决方案2】:

您是将更新后的购物车模型返回到视图中,还是返回没有更新的原始购物车?

回复评论

“成功后我使用 jquery 显示购物车” - 你怎么知道对 AddToCart 的调用成功了?如果它是一个 void 方法,那么除非调用错误 (404/500),否则 jQuery 调用无法知道它是否成功。您是否遇到了竞态条件(异步编程的乐趣)?

例如:

  1. jQuery 调用 AddToCart 开始处理。
  2. Void 方法,jQuery 不等待“响应”(不会有响应),所以交给 Success 方法。
  3. jQuery 调用 ShowCart(或类似的)返回未更新的购物车。
  4. AddToCart 完成,将更新后的购物车版本保存到数据库中。
  5. 下次 ShowCart 运行时,它会返回更新后的购物车。

【讨论】:

  • 在我的控制器中,我有 void 类型的 AddToCart 方法,它将产品添加到购物车(从模型 AddToCart 调用来自购物车 repo 的方法)。要将产品添加到购物车,我正在使用 jquery,它使用指定参数从控制器调用方法 AddToCart。成功后,我使用 jquery 显示购物车 - 调用返回未更新的购物车视图的方法...
  • 在第 5 段中,您写道,下次运行 ShowCart 时应返回更新的视图。但是在我的应用程序中,仅在 vs 中重新启动应用程序会导致更新购物车视图...在购物车存储库中,我正在使用更新实体的 SaveChanges() 方法。将产品添加到购物车后执行应用程序时,我看到数据库中的正确记录已正确更新,但为什么视图没有获得更新的数据?
  • @crocodillez:不,以这种方式更新并不是一个坏主意,我只是建议您从 AddToCart 方法中实际返回一些东西(即更新的购物车对象)。
  • @crocodillez:好的,我明白你的意思了。听起来您只加载了购物车的数据一次,可能加载到与数据库断开连接的对象中-您使用什么来与数据库对话-LINQ to SQL/Entity Framework、其他一些ORM等? 没有看到您正在使用的代码我只能猜测问题所在 - 您使用的是未正确更新的静态对象吗?等等
【解决方案3】:

我正在使用 LINQ to Entity。 从购物车存储库添加方法:

    public void Add(int product, int quantity, string user)
    {
        Cart cart = null;
        cart = (from c in de.Cart
                where (c.userName == "testUser" && c.productId == product)
                select c).First();
        // query is searching for existing product of testUser and id specified in parameter in cart and get it

        cart.quantity += 1; //increment quantity

        de.SaveChanges();   // save entity
    }

来自控制器的 AddToCart 方法:

    public void AddToCart(int pid, int quant, string usr)
    {
        _cartRep.Add(pid,quant,usr);
    }

返回购物车视图的方法:

    public ActionResult Cart()
    {
        IEnumerable<CartInfo> model = _cartRep.GetTrans();
        return View(model);
    }

【讨论】:

  • 下次只需编辑您原来的问题,别担心,我已经为您完成了,就这一次;)
猜你喜欢
  • 1970-01-01
  • 2016-05-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-07
  • 2016-07-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多