尝试访问和编辑数据库中的数据

时间:2017-05-25 12:47:22

标签: c# asp.net-mvc-5

订单控制器

创建并保存订单

   public async Task<ActionResult> FirstClassCreate(FormCollection values)
    {

        var order = new Order();
        TryUpdateModel(order);
        var customer = db.Users.FirstOrDefault(x => x.Email == User.Identity.Name);
        var cart = ShoppingCart.GetCart(this.HttpContext);



        try
        {

            order.DeliveryDate = DateTime.Now.AddDays(1);

            order.DeliveryMethod = "First Class";
            order.FirstName = customer.FirstName;
            order.LastName = customer.LastName;
            order.PostalCode = customer.PostalCode;
            order.State = customer.State;
            order.City = customer.City;
            order.Email = customer.Email;
            order.Country = customer.Country;
            order.Phone = customer.PhoneNumber;
            order.Address = customer.Address;
            order.HasPaid = false;
            order.Username = customer.Email;
            order.OrderDate = DateTime.Now;
            var currentUserId = User.Identity.GetUserId();
            order.Total = cart.GetFirstClass();

            if (order.SaveInfo && !order.Username.Equals("guest@guest.com"))
            {

                var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));
                var store = new UserStore<ApplicationUser>(new ApplicationDbContext());
                var ctx = store.Context;
                var currentUser = manager.FindById(User.Identity.GetUserId());



                //Save this back
                //http://stackoverflow.com/questions/20444022/updating-user-data-asp-net-identity
                //var result = await UserManager.UpdateAsync(currentUser);
                await ctx.SaveChangesAsync();

                await storeDB.SaveChangesAsync();
            }


            //Save Order
            storeDB.Orders.Add(order);
            await storeDB.SaveChangesAsync();
            //Process the order
            cart = ShoppingCart.GetCart(this.HttpContext);
            order.Total = cart.GetFirstClass();

            order = cart.CreateOrder(order);


            return RedirectToAction("FirstClass", "Checkouts", order);



        }
        catch
        {
            //Invalid - redisplay with errors
            return View(order);
        }
    }

结帐控制器

我需要获取上面创建的订单并编辑它以使'HasPaid'等于真

    public ActionResult CreateFirstClass(FormCollection collection)
    {
        var gateway = config.GetGateway();
        Decimal amount;

        //Need to get the amount

        try
        {
            amount = Convert.ToDecimal(Request["amount"]);
        }
        catch (FormatException e)
        {
            TempData["Flash"] = "Error: 81503: Amount is an invalid format.";
            return RedirectToAction("New");
        }

        string nonceFromTheClient = collection["payment_method_nonce"];

        var cart = ShoppingCart.GetCart(this.HttpContext);

我试图获得以下订单但有很多问题。任何有关这方面的帮助将不胜感激

        int? id order.OrderId;
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        Order order = db.Orders.FindAsync(id);
        if (order == null)
        {
            return HttpNotFound();
        }
        if (ModelState.IsValid)
        {
            order.HasPaid = true;
            db.Entry(order).State = EntityState.Modified;
             db.SaveChangesAsync();
        }

.........

0 个答案:

没有答案