路由导致Null Entry

时间:2016-05-11 15:03:12

标签: c# asp.net-mvc entity-framework asp.net-mvc-4 paypal

在我的项目中,用户可以选择通过paypal付款,然后将其带到paypal网站。完成用户后,返回我的网站,直接进入“选择地址”页面。此时我收到了错误;

  

参数字典包含方法'System.Web.Mvc.ActionResult CheckoutPaypal(System.Web.Mvc.FormCollection,System.String,Int32)的非可空类型'System.Int32'的参数'Address'的空条目)'in'T_shirt_Company_v3.Controllers.CheckoutController'。可选参数必须是引用类型,可空类型,或者声明为可选参数。

我的webconfig中的

是返回地址;

<add key="return" value="http://localhost:57564/Checkout/CheckoutPayPal" />

以下是“选择地址

的控制器
 public ActionResult CheckoutPaypal()
        {
            var cart = ShoppingCart.GetCart(this.HttpContext);
            if (!cart.GetCartItems().Any())
                return RedirectToAction("Index", "Store");

            ViewBag.standard = cart.GetTotal();
            ViewBag.fclass = cart.GetTotal() + 5;
            ViewBag.fourty = cart.GetTotal() + 10;


            return View();
        }

        /// <summary>
        /// Gets the address and payment from the user
        /// </summary>
        /// <param name="values">address, payment, contact details</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult CheckoutPaypal(FormCollection values, string id, int Address)
        {

            var order = new Order();
            TryUpdateModel(order);


            var deliveryAddress = storeDB.deliveryAddresses.Where(x => x.AdressId == Address).FirstOrDefault();


            order.Address = deliveryAddress.Address;
            order.City = deliveryAddress.City;
            order.Country = deliveryAddress.Country;
            order.PostalCode = deliveryAddress.PostalCode;
            order.FirstName = deliveryAddress.FirstName;
            order.LastName = deliveryAddress.LastName;
            order.Phone = deliveryAddress.Phone;
            order.Email = User.Identity.GetUserName();
            order.EmailConfirm = User.Identity.GetUserName();
            order.PaymentTransactionId = "Paid with paypal";
            //sets date and username
            order.Username = User.Identity.Name;
            order.OrderDate = DateTime.Now;


            //Order gets saved
            storeDB.Orders.Add(order);
            storeDB.SaveChanges();


            //Order gets processed
            var cart = ShoppingCart.GetCart(this.HttpContext);
            cart.CreateOrder(order);

            //Save again, total not saving properly until now
            storeDB.SaveChanges();


            var postageno = order.PostageList.ToString();
            //adds the postage cost onto the total and updates the database
            if (postageno == "FirstClass")
            {
                decimal addpostage = 5;
                order.Total = order.Total + addpostage;
                storeDB.SaveChanges();
            }
            else if (postageno == "TwentyFourHour")
            {
                decimal addpostage = 10;
                order.Total = order.Total + addpostage;
                storeDB.SaveChanges();
            }


            //Used to change quantity after order made and saved
            using (TshirtStoreDB db = new TshirtStoreDB())
            {
                var details = db.OrderDetails.Where(od => od.OrderId == order.OrderId);

                foreach (var od in details)
                {
                    Product prod = (from p in db.Products where p.ProductId == od.ProductId select p).Single();

                    prod.Quantity -= od.Quantity;
                }

                db.SaveChanges();
            }


            return RedirectToAction("Complete",
                        new { id = order.OrderId });
        }

1 个答案:

答案 0 :(得分:0)

我发现解决此问题的最佳方法是重定向到空白页面,然后立即重定向回结帐页面,这样就删除了错误并运行良好