コード例 #1
0
        public ActionResult Index(ProfileViewModel profileInfo)
        {
            //if (ModelState.IsValid)
            //{
                using (InternetStoreDBContext dbc = new InternetStoreDBContext())
                {
                    var newUserInfo = new Classes.User();
                    var oldUserInfo = (from u in dbc.Users where u.Email == profileInfo.Email select u).ToList().FirstOrDefault();
                    if (oldUserInfo != null)
                    {
                        dbc.Users.DeleteOnSubmit(oldUserInfo);
                        dbc.SubmitChanges();

                        //Currently constant fields:
                        newUserInfo.ID = oldUserInfo.ID;
                        newUserInfo.UserName = oldUserInfo.UserName;
                        newUserInfo.Password = oldUserInfo.Password;
                        //Changable fields:
                        newUserInfo.FirstName = profileInfo.FirstName;
                        newUserInfo.LastName = profileInfo.LastName;
                        newUserInfo.Phone = profileInfo.Phone;
                        newUserInfo.Address = profileInfo.Address;
                        newUserInfo.Email = profileInfo.Email;

                        dbc.Users.InsertOnSubmit(newUserInfo);
                        dbc.SubmitChanges();

                        ProfileViewModel newProfile = new ProfileViewModel();
                        newProfile.UserName = newUserInfo.UserName ?? "";
                        newProfile.FirstName = newUserInfo.FirstName ?? "";
                        newProfile.LastName = newUserInfo.LastName ?? "";
                        newProfile.Email = newUserInfo.Email ?? "";
                        newProfile.Phone = newUserInfo.Phone ?? "";
                        newProfile.Address = newUserInfo.Address ?? "";

                        return View(newProfile);
                    }
                }
            //}
            return View(profileInfo);
        }
コード例 #2
0
        public ViewResult Checkout(CartViewModel cart, User user, Order order)
        {
            if (cart.OrdersDetails.Count() == 0)
            {
                ViewBag.IsSuccess = false;
                ViewBag.Message = "Sorry, your cart is empty!";
            }
            else
            {
                ViewBag.IsSuccess = true;
                ViewBag.Message = "Thanks for placing your order. We'll ship your goods as soon as possible.";

                using (InternetStoreDBContext dbc = new InternetStoreDBContext())
                {
                    if (!User.Identity.IsAuthenticated)
                    {
                        dbc.Users.InsertOnSubmit(user);
                        dbc.SubmitChanges();
                        order.UserID = user.ID;
                    }
                    else
                    {
                        User currentUser = (from u in dbc.Users where u.Email == User.Identity.Name select u).ToList().FirstOrDefault();
                        order.UserID = currentUser.ID;
                    }

                    order.ShippingDate = DateTime.Now.ToOADate();
                    if (order.ShippingStatus == null)
                        order.ShippingStatus = "Check";
                    dbc.Orders.InsertOnSubmit(order);
                    dbc.SubmitChanges();

                    Sale sale = new Sale() { OrderID = order.ID, SalesAmount = cart.ComputeTotalValue() };
                    dbc.Sales.InsertOnSubmit(sale);
                    dbc.SubmitChanges();

                    foreach (var orderDetails in cart.OrdersDetails)
                    {
                        var orderDetails2 = new OrderDetails() { OrderID = order.ID, ProductID = orderDetails.ProductID, Quantity = orderDetails.Quantity };
                        dbc.OrderDetails.InsertOnSubmit(orderDetails2);
                        dbc.SubmitChanges();
                    }

                    cart.Clear();
                }
            }

            ViewBag.IsAuthenticated = User.Identity.IsAuthenticated;
            return View();
        }
コード例 #3
0
        public ActionResult Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                using (InternetStoreDBContext dbc = new InternetStoreDBContext())
                {
                    var userInfo = (from u in dbc.Users where u.Email == model.Email select u).ToList().FirstOrDefault();
                    if (userInfo == null)
                    {
                        var user = new Classes.User()
                        {
                            UserName = model.UserName,
                            FirstName = model.FirstName,
                            LastName = model.LastName,
                            Phone = model.Phone,
                            Email = model.Email,
                            Password = model.Password
                        };

                        dbc.Users.InsertOnSubmit(user);
                        dbc.SubmitChanges();

                        FormsAuthentication.SetAuthCookie(model.Email, false);
                        return RedirectToAction("Index", "Home");
                    }
                    else
                    {
                        ModelState.AddModelError("", "Email address already in use.");
                    }
                }
            }
            // If we got this far, something failed, redisplay form
            return View(model);
        }