public async Task <IActionResult> Register(IFormCollection form) { string userName = form["userName"]; string name = form["name"]; string password = form["password"]; string roleID = "CT"; string confirm = form["confirm"]; if (!password.Equals(confirm)) { TempData["Message"] = "Password not match!"; return(RedirectToAction("RegisterForm", "Login")); } if (_context.TblUsers.SingleOrDefault(s => s.UserName == userName) == null) { TblUser user = new TblUser(); user.UserName = userName; user.Password = MD5_Encryption(password); user.Name = name; user.RoleId = roleID; _context.TblUsers.Add(user); await _context.SaveChangesAsync(); TempData["Message"] = "Create account success!"; return(RedirectToAction("Login", "ShoppingCart")); } TempData["Message"] = "Username had existed!"; return(RedirectToAction("RegisterForm", "Login")); }
public async Task <IActionResult> Create([Bind("ProductId,ProductName,Price,Description,Quantity,ImgSrc,CategoryId")] TblProduct tblProduct) { ViewData["CategoryId"] = new SelectList(_context.TblCategories, "CategoryId", "CategoryId", tblProduct.CategoryId); if (_context.TblProducts.Find(tblProduct.ProductId) != null) { return(RedirectToAction(nameof(Error))); } if (ModelState.IsValid) { _context.Add(tblProduct); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(tblProduct)); }
public async Task <IActionResult> CheckOut(IFormCollection form) { TblUser user = null; Cart cart = null; try { user = JsonConvert.DeserializeObject <TblUser>(HttpContext.Session.GetString("User")); cart = GetCart(); } catch {} List <String> soldOutList = new List <string>(); bool check = true; try { foreach (var item in cart.Items) { TblProduct pro = _context.TblProducts.Find(item._shopping_product.ProductId); if (pro != null) { if (pro.Quantity < item._shopping_quantity) { soldOutList.Add(pro.ProductName + " out of stock! Available " + pro.Quantity + " products! "); check = false; } } } } catch { } if (!check) { TempData["SoldOutList"] = soldOutList; return(RedirectToAction("ShowToCart", "ShoppingCart")); } if (cart != null) { string orderID = GetOrderID(); ViewData["OrderID"] = orderID; string userName = user.UserName; string name = user.Name; ViewData["Name"] = name; string address = form["address"]; ViewData["Address"] = address; string phone = form["phone"]; ViewData["Phone"] = phone; string paymentType = "Cash"; ViewData["Payment_Type"] = paymentType; float totalPrice = (float)cart.TotalMoney(); ViewData["Total_Price"] = totalPrice; DateTime time = DateTime.Now; ViewData["Time"] = time; TblOrder order = new TblOrder(); order.OrderId = orderID; order.UserName = userName; order.Name = name; order.Address = address; order.Phone = phone; order.PaymentType = paymentType; order.TotalPrice = totalPrice; order.Time = time; _context.TblOrders.Add(order); await _context.SaveChangesAsync(); await AddOrderDetails(orderID, cart); HttpContext.Session.Remove("Cart"); } return(View()); }