public ActionResult Create([Bind(Include = "DeviceId,DeviceName,DescriptionDevice,Components,Shop,MadeInCountry,Quantity,Price,file,Category")] TblDevice tblDevice, HttpPostedFileBase file) { if (ModelState.IsValid) { bool fileUploaded = false; if (file != null && file.ContentLength > 0) { var fileName = Path.GetFileName(file.FileName); var path = Path.Combine(Server.MapPath("~/Content/DeviceImages/"), fileName); file.SaveAs(path); fileUploaded = true; } if (fileUploaded) { tblDevice.Picture = file.FileName; db.TblDevices.Add(tblDevice); db.SaveChanges(); return(RedirectToAction("Index")); } else { return(RedirectToAction("Index")); } } ViewBag.Category = new SelectList(db.TblCategories, "CategoryId", "CategoryName", tblDevice.Category); ViewBag.Shop = new SelectList(db.TblShops, "ShopId", "ShopName", tblDevice.Shop); return(View(tblDevice)); }
public ActionResult Create([Bind(Include = "CategoryId,CategoryName,Description,Subcategory")] TblCategory tblCategory) { if (ModelState.IsValid) { db.TblCategories.Add(tblCategory); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(tblCategory)); }
public ActionResult Create([Bind(Include = "ShopId,ShopName,Address,NoOfEmployees,TaxIdentificationNo,DateOfFounding")] TblShop tblShop) { if (ModelState.IsValid) { db.TblShops.Add(tblShop); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(tblShop)); }
public async Task <ActionResult> ChangeEmail(ChangeEmailViewModel model) { if (!ModelState.IsValid) { ViewBag.Message = "Please enter correct information"; return(View(model)); } var user = Session["user"] as TblUser; if (model.OldEmail.Equals(user.Email)) { user.Email = model.NewEmail; zTest2DBEntities db = new zTest2DBEntities(); db.TblUsers.Find(user.UserId).Email = model.NewEmail; db.SaveChanges(); Session["welcome msg"] = "You successfully changed your email address!"; return(RedirectToAction("Index", "Manage")); } else { ViewBag.Message = "Please enter correct information"; return(View(model)); } }
public ActionResult ChangeAdminRights() { int userId = int.Parse(Request["userId"]); zTest2DBEntities db = new zTest2DBEntities(); db.TblUsers.Find(userId).Role = db.TblUsers.Find(userId).Role ? false : true; db.SaveChanges(); return(RedirectToAction("AdminRights", "Account")); }
public async Task <ActionResult> Register(RegisterViewModel model) { if (ModelState.IsValid) { zTest2DBEntities db = new zTest2DBEntities(); TblUser newUser = new TblUser(); newUser.UserName = model.UserName; newUser.Email = model.Email; newUser.FirstName = model.Name; newUser.LastName = model.LastName; newUser.Phone = model.Phone; var salt = MakeSalt(SaltLength); newUser.Salt = salt; newUser.HashedPass = ComputeHash(model.Password, salt); db.TblUsers.Add(newUser); try { db.SaveChanges(); } catch (DbEntityValidationException e) { Console.WriteLine(e); } Session["user"] = (db.TblUsers.Select(x => x).OrderByDescending(x => x.UserId).Take(1)).ToList()[0]; Session["welcome msg"] = "Hello " + newUser.FirstName + "!"; return(RedirectToAction("Index", "Manage")); } return(View(model)); }
public async Task <ActionResult> ChangeLastName(ChangeLastNameViewModel model) { if (!ModelState.IsValid) { return(View(model)); } var user = Session["user"] as TblUser; if (model.OldLastName.Equals(user.LastName)) { user.LastName = model.NewLastName; zTest2DBEntities db = new zTest2DBEntities(); db.TblUsers.Find(user.UserId).LastName = model.NewLastName; db.SaveChanges(); } return(RedirectToAction("Index", "Manage")); }
public async Task <ActionResult> ChangePassword(ChangePasswordViewModel model) { if (!ModelState.IsValid) { return(View(model)); } var user = Session["user"] as TblUser; if (model.OldPassword.Equals(user.Password) && model.NewPassword.Equals(model.ConfirmPassword)) { user.Password = model.NewPassword; zTest2DBEntities db = new zTest2DBEntities(); db.TblUsers.Find(user.UserId).Password = model.NewPassword; db.SaveChanges(); } return(View(model)); }
public string Add() { int id = int.Parse(Request["id"]); int amount = int.Parse(Request["amount"]); TblCart usersCart = null; foreach (TblCart cart in storeDB.TblCarts) { if (cart.UserId == (Session["user"] as TblUser).UserId) { usersCart = cart; break; } } if (usersCart == null) { usersCart = new TblCart(); usersCart.UserId = (Session["user"] as TblUser).UserId; storeDB.TblCarts.Add(usersCart); storeDB.SaveChanges(); } TblCartItem usersCartItem = null; foreach (TblCartItem item in usersCart.TblCartItems) { if (item.DeviceId == id) { usersCartItem = item; break; } } if (usersCartItem == null) { usersCartItem = new TblCartItem(); usersCartItem.DeviceId = id; usersCartItem.Amount = amount; usersCartItem.CartId = usersCart.CartId; usersCart.TblCartItems.Add(usersCartItem); storeDB.SaveChanges(); } else { usersCartItem.Amount += amount; storeDB.SaveChanges(); } string response_amount = ""; foreach (TblDevice device in storeDB.TblDevices) { if (device.DeviceId == id) { device.Quantity -= amount; response_amount = "" + device.Quantity; break; } } storeDB.SaveChanges(); return(response_amount); }