我想了解如何通过值传递的模型绑定有效

时间:2012-11-26 17:33:19

标签: asp.net-mvc

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

我想了解如果使用模型绑定器通过值传递此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 });
    }

希望这能解释我的问题!

感谢您的帮助

详情

此粗体代码不会影响会话中的customprofile

   [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);**

1 个答案:

答案 0 :(得分:1)

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

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

另请参阅:ByRef vs ByVal Clarification