Example #1
0
        public ActionResult CreateProcurement([Bind(Include = "ProductId,PurchaseDate,Quantity,UnitPrize")] Procurement procurement)
        {
            ProductRepository           productRepo     = new ProductRepository(db);
            ProcurementRepository       procurementRepo = new ProcurementRepository(db);
            List <ProcurementViewModel> procurements    = new List <ProcurementViewModel>();

            //將輸入資料儲存在資料庫
            if (ModelState.IsValid)
            {
                db.Procurement.Add(procurement);
                db.SaveChanges();
            }
            //抓取改變後的資料
            foreach (Procurement item in procurementRepo.GetAll().ToList())
            {
                ProcurementViewModel procurementVM = new ProcurementViewModel()
                {
                    ProductName   = productRepo.GetProductNameByID(item.ProductId),
                    ProcurementId = item.ProcurementId,
                    PurchaseDate  = item.PurchaseDate.ToString("yyyy/MM/dd hh:mm:ss"),
                    Quantity      = item.Quantity,
                    UnitPrize     = item.UnitPrize,
                };
                procurements.Add(procurementVM);
            }
            return(Json(procurements));
        }
Example #2
0
        public ActionResult Create([Bind(Include = "ProcurementId,ProductId,PurchaseDate,Quantity,UnitPrize")] Procurement procurement)
        {
            if (ModelState.IsValid)
            {
                db.Procurement.Add(procurement);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            ViewBag.ProductId = new SelectList(db.Products, "ProductId", "Name", procurement.ProductId);
            return(View(procurement));
        }
Example #3
0
        // GET: Order
        public ActionResult Index()
        {
            OrderViewModel orderViewModel = (OrderViewModel)Session["ReceiverData"];

            if (orderViewModel != null)
            {
                LederContext db     = new LederContext();
                Order        orders = new Order
                {
                    OrderId         = Guid.NewGuid(),
                    Email           = orderViewModel.Email,
                    RecieverName    = orderViewModel.RecieverName,
                    RecieverPhone   = orderViewModel.RecieverPhone,
                    RecieverAddress = orderViewModel.RecieverAddress,
                    RecieverZipCode = orderViewModel.RecieverZipCode,
                    OrderDate       = DateTime.Now,
                    TotalAmount     = orderViewModel.Carts.TotalAmount,
                    PayStatus       = orderViewModel.PayStatus,
                    OrderStatus     = "未出貨"
                };
                db.Orders.Add(orders);

                foreach (var item in orderViewModel.CartItems)
                {
                    db.OrderDetails.Add(new OrderDetail {
                        OrderId = orders.OrderId, ProductId = item.Id, Price = item.Price, Quantity = item.Quantity, Amount = (decimal)item.Amount
                    });
                }

                db.SaveChanges();
                shoppingCart.ClearCart();
            }

            return(View());
        }
Example #4
0
        public async Task <ActionResult> Register(RegisterViewModel model)
        {
            var db = new LederContext();//實體化存取資料庫的EntityframeWork

            if (ModelState.IsValid)
            {
                var user = new User {
                    UserName = model.Email, Email = model.Email, PhoneNumber = model.CellPhone
                };                                                                                              //拿來接User的資料
                var userdetail = new UserDetail {
                    Address = model.Address, BirthDay = model.BirthDate, IdentityCard = model.IdentityCard, ShipAddress = model.ShipAddress, Email = model.Email
                };                                                                                                                                                                            //拿來接UserDetail的資料
                var result = await UserManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false); //User經由Identity寫入資料庫

                    db.UserDetail.Add(userdetail);                                                        //UserDetail經由EF寫入資料庫
                    try
                    {
                        db.SaveChanges();
                    }
                    catch (System.Data.Entity.Validation.DbEntityValidationException dbEx) //當初為了除錯而增加的,日後維護也方便
                    {
                        Exception raise = dbEx;
                        foreach (var validationErrors in dbEx.EntityValidationErrors)
                        {
                            foreach (var validationError in validationErrors.ValidationErrors)
                            {
                                string message = string.Format("{0}:{1}",
                                                               validationErrors.Entry.Entity.ToString(),
                                                               validationError.ErrorMessage);
                                // raise a new exception nesting
                                // the current instance as InnerException
                                raise = new InvalidOperationException(message, raise);
                            }
                        }
                        throw raise;
                    }


                    // For more information on how to enable account confirmation and password reset please visit https://go.microsoft.com/fwlink/?LinkID=320771
                    // Send an email with this link
                    // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
                    // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                    // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");

                    return(RedirectToAction("Index", "Home"));
                }

                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
Example #5
0
        public async Task <ActionResult> ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, string returnUrl)
        {
            var db = new LederContext();

            if (User.Identity.IsAuthenticated)
            {
                return(RedirectToAction("Index", "Manage"));
            }

            if (ModelState.IsValid)
            {
                // 從外部登入提供者處取得使用者資訊
                var info = await AuthenticationManager.GetExternalLoginInfoAsync();

                if (info == null)
                {
                    return(View("ExternalLoginFailure"));
                }
                var user = new User {
                    UserName = model.Email, Email = model.Email
                };
                var userdetail = new UserDetail {
                    Address = model.Address, Email = model.Email, BirthDay = model.BirthDate, IdentityCard = model.IdentityCard, ShipAddress = model.ShipAddress
                };
                var result = await UserManager.CreateAsync(user);

                if (result.Succeeded)
                {
                    db.UserDetail.Add(userdetail);
                    db.SaveChanges();
                    result = await UserManager.AddLoginAsync(user.Id, info.Login);

                    if (result.Succeeded)
                    {
                        await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false);

                        return(RedirectToLocal(returnUrl));
                    }
                }
                AddErrors(result);
            }

            ViewBag.ReturnUrl = returnUrl;
            return(View(model));
        }
Example #6
0
        public ActionResult EditUserDetail([Bind(Include = "UserDetailID,Address,ShipAddress,BirthDay,IdentityCard,Email")]//用Bind使模型繫結在這六個欄位上
                                           UserDetail userDetail)
        {
            if (ModelState.IsValid)                                //如果改動成功
            {
                db.Entry(userDetail).State = EntityState.Modified; //告訴EF狀態為改動成功

                db.SaveChanges();                                  //儲存改動
                return(RedirectToAction("Index"));                 //回到管理帳號主頁面(Manager,Index)
            }
            return(View(userDetail));
            //不要理下面註解。。。
            //var result = await UserManager.AddPasswordAsync(User.Identity.GetUserId(), model.NewPassword);
            //if (result.Succeeded)
            //{
            //    var user = await UserManager.FindByIdAsync(User.Identity.GetUserId());
            //    if (user != null)
            //    {
            //        await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);
            //    }
            //    return RedirectToAction("Index", new { Message = ManageMessageId.SetPasswordSuccess });
            //}
            //AddErrors(result);
        }