public void DeleteElement(TEntity obj) { var o = dbSet.Remove(obj); context.SaveChanges(); context.Entry(obj).State = EntityState.Detached; }
[ValidateAntiForgeryToken] //Μην ξεχάσεις @Html.AntiForgeryToken() στο view public ActionResult EditSubmitProduct(Product product) { if (!ModelState.IsValid) { return(View("Edit", product)); } Product dProduct = new Product(); using (App_Context db = new App_Context()) { dProduct = db.Products.Include("Category").SingleOrDefault(d => d.ProductId == product.ProductId); if (dProduct == null) { return(HttpNotFound()); } else { dProduct.ProductName = product.ProductName; dProduct.ProductPrice = product.ProductPrice; dProduct.ImagePath = product.ImagePath; dProduct.CategoryId = product.CategoryId; db.Entry(dProduct.Category).State = System.Data.Entity.EntityState.Unchanged; db.SaveChanges(); } return(RedirectToAction("Product", "Product")); } }
public ActionResult Edit([Bind(Include = "Id,Content,LastEdit")] Comment comment) { if (ModelState.IsValid) { db.Entry(comment).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } return(View(comment)); }
public ActionResult Edit([Bind(Include = "Id,Title,Description,LastEdit")] Domain domain) { domain.LastEdit = DateTime.Now; if (ModelState.IsValid) { db.Entry(domain).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } return(View(domain)); }
public ActionResult LiftBan(int id) { User unBannedUser = new User(); Role unBannedRole = new Role(); using (App_Context db = new App_Context()) { unBannedUser = db.Users.Include("Role").SingleOrDefault(u => u.UserId == id); if (unBannedUser == null) { return(HttpNotFound()); } unBannedRole = db.Roles.Find(2); unBannedUser.Role = unBannedRole; db.Entry(unBannedRole).State = System.Data.Entity.EntityState.Unchanged; db.SaveChanges(); } return(RedirectToAction("Dashboard", "Admin")); }
public ActionResult CreateSubmitProduct(Product productNew) { if (!ModelState.IsValid) { return(View("Create", productNew)); } Product product = new Product(); Category category = new Category(); using (App_Context db = new App_Context()) { category = db.Categories.Find(productNew.CategoryId); product.ProductName = productNew.ProductName; product.ProductPrice = productNew.ProductPrice; product.Category = category; product = productNew; db.Products.Add(productNew); db.Entry(category).State = System.Data.Entity.EntityState.Unchanged; db.SaveChanges(); } return(RedirectToAction("Product", "Product")); }
public async Task <ActionResult> Register(User user) { if (!ModelState.IsValid) { return(View(user)); } bool admincheck = false; using (App_Context db = new App_Context()) { if (db.Users.ToList().Count == 0) { admincheck = true; } var existUser = db.Users.Where(i => i.Username == user.Username).ToList(); var count = existUser.Count; if (count > 0) { ViewData["UserExist"] = existUser; return(View(user)); } bool isValidPass = Regex.IsMatch(user.Password, passPattern); if (!isValidPass && user.Username != "god") { ViewData["InvalidPass"] = existUser; return(View(user)); } if (count == 0) { var salt = Password.GetSalt(); var hash = Password.Hash(user.Password, salt); Role r = new Role(); if (admincheck == true) { r = db.Roles.Find(1); } else { r = db.Roles.Find(2); } User u = new User { Username = user.Username, Password = Convert.ToBase64String(hash), Salt = Convert.ToBase64String(salt), Firstname = user.Firstname, Lastname = user.Lastname, Phone = user.Phone, Address = user.Address, City = user.City, Email = user.Email, PostalCode = user.PostalCode, Role = r }; db.Users.Add(u); db.Entry(r).State = System.Data.Entity.EntityState.Unchanged; db.SaveChanges(); ViewData["Success"] = existUser; await emailHandler.RegistrationEmail(user.Email, user.Firstname, user.Username, user.Password); } } return(View()); }
public ActionResult Create(ArticleVersionViewModel avViewModel) { avViewModel.AllDomains = GetDomains(); Article a = new Article(); Version v = new Version(); // domains var ds = db.Domains.Where(d => avViewModel.DomainIds.Contains(d.Id)); if (avViewModel.DomainIds != null && avViewModel.DomainIds.Any()) { if (ds != null && ds.Any()) { a.Domains = ds.ToList(); } else { ViewBag.Message = "Couldn't find selected domains."; return(View(avViewModel)); } } // write file contents if (avViewModel.ContentFile != null && avViewModel.ContentFile.ContentLength > 0) { string content = ReadStream(avViewModel.ContentFile.InputStream, avViewModel.ContentFile.ContentLength); try { v.ContentPath = WriteFile(content); } catch (Exception e) { ViewBag.Message = "Error saving the file."; return(View(avViewModel)); } } else if (avViewModel.Content != null && avViewModel.Content.Length > 0) { try { v.ContentPath = WriteFile(avViewModel.Content); } catch (Exception e) { ViewBag.Message = "Error saving the file."; return(View(avViewModel)); } } else { ModelState.AddModelError("OneOfTwoFieldsShouldBeFilled", "Either you upload a file or type your article in the box. You can't have an empty article."); return(View(avViewModel)); } v.Title = avViewModel.Title; a.ProtectFromEditing = false; v.LastEdit = DateTime.Now; a.DatePublished = DateTime.Now; if (TryValidateModel(a)) { db.Articles.Add(a); db.SaveChanges(); v.Article = a; db.Versions.Add(v); db.SaveChanges(); a.CurrentVersionId = v.Id; db.Entry(a).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } // if we're here, something failed System.IO.File.Delete(v.ContentPath); // cleanup unused files return(View(avViewModel)); }