0

我认为这里有一个棘手的问题!

我想了解如果这个 Cart 对象是如何使用模型绑定器按值传递的,那么它如何影响会话中的实际值?

  public RedirectToRouteResult AddToCart(Cart cart, int productId, string returnUrl) {
        Product product = repository.Products
            .FirstOrDefault(p => p.ProductID == productId);

        if (product != null) {
            cart.AddItem(product, 1);
        }
        return RedirectToAction("Index", new { returnUrl });
    }

希望这能解释我的问题!

感谢您的帮助

细节

此粗体代码不会影响会话中的自定义配置文件

   [AllowAnonymous]
    [HttpPost]
    public ActionResult Register(RegisterModel model, CustomProfile customProfile)
    {
        if (User.Identity.IsAuthenticated)
            return RedirectToAction("Index", "Home");
        if (ModelState.IsValid)
        {
            // Attempt to register the user
            MembershipCreateStatus createStatus;


            membershipProvider.CreateUser(model, null, null, true, null,
                                          out createStatus);

            if (createStatus == MembershipCreateStatus.Success)
            {
                const string message = "You have successfully reigestered, and you are now logged in.";
                authProvider.SetAuthCookie(model.UserName, false /* createPersistentCookie */);

                var newCustomProfile = new CustomProfile(model.UserName);
                var db = new CrowdFundingDB();

                **customProfile = customProfileProvider.Create(newCustomProfile);**
4

1 回答 1

1

Cart不是值类型,而是引用类型

将它作为参数传递不会复制对象,该方法接收对实际对象的引用的副本。它将通过复制的引用访问同一个对象。

另请参阅:ByRef 与 ByVal 澄清

于 2012-11-26T17:51:55.023 回答