public ActionResult Create([Bind(Include = "CategoryId,CategoryName,Alias,Description,Order,status,ParentId,CreateDate")] Category category) { if (ModelState.IsValid) { category.ParentId = (category.ParentId == null) ? 0 : category.ParentId; category.CreateDate = DateTime.Now; category.Alias = ConvertToAlias.ConvertTitle(category.CategoryName); db.Category.Add(category); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(category)); }
public ActionResult Details(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } ProductDetail productDetail = db.ProductDetail.Find(id); if (productDetail == null) { return(HttpNotFound()); } productDetail.Product.View++; db.SaveChanges(); //get list size and color from productDetailId List <string> listColor = db.ProductDetail.Where(r => r.ProductId == productDetail.ProductId).Select(r => r.Color).Distinct().ToList(); List <string> listSize = db.ProductDetail.Where(r => r.ProductId == productDetail.ProductId && r.Color == productDetail.Color).Select(r => r.Size).Distinct().ToList(); ViewBag.ListColor = listColor; ViewBag.listSize = listSize; //get sản phẩm liên quan var ListProduct = db.Product.Where(r => r.CategoryId == productDetail.Product.CategoryId && r.ProductId != productDetail.ProductId).Take(4).ToList(); ViewBag.ListProduct = ListProduct; return(View(productDetail)); }
public ActionResult AddressAndPayment(Order order) { var cart = ShoppingCart.GetCart(this.HttpContext); if (ModelState.IsValid) { order.Username = Session["Email"].ToString(); order.OrderDate = DateTime.Now; //Save Order db.Orders.Add(order); db.SaveChanges(); //Process the order cart.CreateOrder(order); return(RedirectToAction("Complete", new { id = order.OrderId })); } // Set up our ViewModel var viewModel = new ShoppingCartViewModel { CartItems = cart.GetCartItems(), CartTotal = cart.GetTotal() }; ViewBag.CountCart = cart.GetCount(); ViewBag.Cart = viewModel; return(View(order)); }
public ActionResult Create([Bind(Include = "Username,Password,FullName,Email,Avatar,IsAdmin,Allowed")] CreateViewModel account) { if (!ModelState.IsValid) { return(View(account)); } Account acc = new Account() { FirstName = account.Username, Password = Common.EncryptMD5(account.Username + account.Password), Email = account.Email, //Role = account., Allowed = account.Allowed, Avatar = "avatardefault.jpg" }; db.Account.Add(acc); db.SaveChanges(); return(RedirectToAction("Index")); }
public ActionResult Register(RegisterView register, string returnUrl) { if (ModelState.IsValid) { string passwordMD5 = KidShop.Areas.Admin.Models.BusinessModel.Common.EncryptMD5(register.Email + register.Password); Account acc = new Account { LastName = register.LastName, FirstName = register.FirstName, Email = register.Email, Password = passwordMD5, Role = 50, Allowed = true }; db.Account.Add(acc); db.SaveChanges(); Session["Email"] = register.Email; Session["LastName"] = register.LastName; Session["FirstName"] = register.FirstName; return(RedirectToLocal(returnUrl)); } return(View(register)); }
public ActionResult Register(RegisterViewModel acc) { if (ModelState.IsValid) { Account temp = new Account() { Username = acc.Username, Password = Common.EncryptMD5(acc.Username + acc.Password), Email = acc.Email, FullName = acc.FullName, Avatar = "avatardefault.jpg", isAdmin = false, Allowed = true }; db.Account.Add(temp); db.SaveChanges(); TempData["RegisterSuccess"] = "Đăng ký thành công. Vui lòng đăng nhập để tiếp tục!"; return(RedirectToAction("Login", "Account")); } return(View(acc)); }
public ActionResult Create(/*[Bind(Include = "ProductId,ProductName,Description,CategoryId,Price,Qty,Status")]*/ Product product /*, HttpPostedFileBase Image*/) { // Lấy tổng sản phẩm var tags_size = Request.Form["tags_size"].Split(','); var tags_color = Request.Form["tags_color"].Split(','); int Qty_detail = 0; for (int i = 0; i < tags_color.Length; i++) { for (int j = 0; j < tags_size.Length; j++) { if (Request.Form["qty_detail[" + tags_color[i] + "-" + tags_size[j] + "]"] != null) { try { Qty_detail += Int16.Parse(Request.Form["qty_detail[" + tags_color[i] + "-" + tags_size[j] + "]"]); } catch { ModelState.AddModelError("ProductDetails", "Số lượng nhập ko hợp lệ"); } } } } if (ModelState.IsValid) { product.Qty = (Qty_detail == 0) ? product.Qty : Qty_detail; //Lưu ảnh detail sản phẩm vào server List <string> listFileName = new List <string>(); if (Session["fileUpload"] != null) { fileUpload = (List <HttpPostedFileBase>)Session["fileUpload"]; foreach (var item in fileUpload) { if (item != null && item.ContentLength > 0) { string extensionFile2 = item.FileName.Substring(item.FileName.LastIndexOf(".")); string newfilename2 = Common.EncryptMD5(DateTime.Now.ToBinary().ToString()) + extensionFile2; string path2 = Path.Combine(Server.MapPath("~/Areas/Admin/Content/Images/ProductImages"), newfilename2); item.SaveAs(path2); listFileName.Add(newfilename2); } } } if (listFileName != null) { product.Image = listFileName[0]; } Session["fileUpload"] = null; db.Product.Add(product); db.SaveChanges(); //Lấy id sản phẩm int lastProductId = db.Product.Max(x => x.ProductId); //Lưu ảnh detail sản phẩm vào CSDL foreach (var item in listFileName) { ProductImage a = new ProductImage(); a.ProductId = lastProductId; a.ImageName = item; db.ProductImage.Add(a); } db.SaveChanges(); //Lưu chi tiết sản phẩm for (int i = 0; i < tags_color.Length; i++) { for (int j = 0; j < tags_size.Length; j++) { if (Request.Form["qty_detail[" + tags_color[i] + "-" + tags_size[j] + "]"] != null && Request.Form["price_detail[" + tags_color[i] + "-" + tags_size[j] + "]"] != null) { db.ProductDetail.Add(new ProductDetail() { ProductId = lastProductId, Color = tags_color[i], Size = tags_size[j], Qty = (Request.Form["qty_detail[" + tags_color[i] + "-" + tags_size[j] + "]"] != null) ? Int16.Parse(Request.Form["qty_detail[" + tags_color[i] + "-" + tags_size[j] + "]"]) : 0, Price = (Request.Form["price_detail[" + tags_color[i] + "-" + tags_size[j] + "]"] != null) ? Decimal.Parse(Request.Form["price_detail[" + tags_color[i] + "-" + tags_size[j] + "]"]) : 0 }); } } } db.SaveChanges(); return(RedirectToAction("Index")); } else { Session["fileUpload"] = null; } ViewBag.CategoryId = new SelectList(db.Category, "CategoryId", "CategoryName", product.CategoryId); return(View(product)); }