Asp.net mvc 我想了解使用传递值的模型绑定是如何工作的

Asp.net mvc 我想了解使用传递值的模型绑定是如何工作的,asp.net-mvc,Asp.net Mvc,这里有个棘手的问题,我想 我想了解如果使用模型绑定器通过值传递此Cart对象,那么它如何影响会话中的实际值 public RedirectToRouteResult AddToCart(Cart cart, int productId, string returnUrl) { Product product = repository.Products .FirstOrDefault(p => p.ProductID == productId);

这里有个棘手的问题,我想

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

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

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


另请参见:

不清楚您在问什么。基本上,如果它是通过值传递的,我的理解是它创建了原始值的副本,然后如果修改此副本,它将不会影响会话中的原始值。thx,我不明白这是如何通过引用传递的:public object BindModel(ControllerContext ControllerContext,ModelBindingContext bindingContext){//从会话购物车获取购物车购物车=(购物车)ControllerContext.HttpContext.session[sessionKey];//如果(购物车==null)会话数据中没有购物车,则创建购物车{cart=new cart();controllerContext.HttpContext.Session[sessionKey]=cart;}//返回购物车返回购物车;}那么这为什么不影响会话中的自定义配置文件:public ActionResult Register(RegisterModel,CustomProfile CustomProfile){customProfile=customProfileProvider.Create(newCustomProfile);@Quantum请在您的问题中发布您的代码。我无法在评论中阅读它。