public ActionResult Create([Bind(Include = "Id,DataInHttpPostedFileBase")] AdModels adModels) { if (ModelState.IsValid) { Picture picture = new Picture() { Pic = ConvertHttpPostedFileBaseToByteArray(adModels.DataInHttpPostedFileBase) }; db.Picture.Add(picture); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(adModels)); }
public ActionResult Create(AdModels adModels) { if (ModelState.IsValid) { //instanciating the ad class and mapping the properties Ad ad = new Ad(); ad.Title = adModels.Title; ad.District = adModels.District; ad.Street = adModels.Street; if (adModels.Description.Length > 1000) /*correctionNeeded*/ { ModelState.AddModelError(nameof(adModels.Description), "A mező nem fogad többet 1000 karakternél"); return(View(adModels)); } ad.Description = adModels.Description; ad.Price = adModels.Price; ad.Size = adModels.Size; ad.UserId = int.Parse(Session["UserId"]?.ToString()); if (adModels.DataInHttpPostedFileBase == null) { ModelState.AddModelError(nameof(adModels.DataInHttpPostedFileBase), "Kép feltöltése kötelező"); return(View()); } //instanciating the picture class and mapping the properties Picture picture = new Picture() { Pic = ConvertHttpPostedFileBaseToByteArray(adModels.DataInHttpPostedFileBase) }; ad.PictureId = picture.Id; //adding both the picture and the ad to the database db.Picture.Add(picture); db.Ad.Add(ad); db.SaveChanges(); return(RedirectToAction("Index", "Home")); } return(View(adModels)); }
public ActionResult Create(UserModels model) { if (ModelState.IsValid) { User user = new RealEstateProjectUI.User(); var salt = Crypto.GenerateSalt(); var hashedPassword = Crypto.SHA256(model.Password + salt); var userList = db.User.Select(u => u.UserName).ToList(); var emailList = db.User.Select(e => e.Email).ToList(); user.UserName = model.UserName; user.Email = model.Email; user.Salt = salt; if (model.Password != model.ConfirmPassword) { ModelState.AddModelError(nameof(model.ConfirmPassword), "A jelszavak nem egyeznek."); return(View(model)); } if (userList.Contains(model.UserName)) { ModelState.AddModelError(nameof(model.UserName), "Ez a felhasználónév már foglalt."); return(View(model)); } if (emailList.Contains(model.Email)) { ModelState.AddModelError(nameof(model.Email), "Ez az email cím már foglalt."); return(View(model)); } if (model.ConfirmPassword == model.Password) { user.Password = hashedPassword; } //user.UserRoleId = model.UserRoleId; if (model.Email == "*****@*****.**") { user.UserRoleId = 1; } else { user.UserRoleId = 2; } db.User.Add(user); db.SaveChanges(); //update the session objects for further authorization if (user != null) { if (model.UserRoleId == 1) { Session["UserRole"] = "admin"; } else { Session["UserRole"] = "advertiser"; } Session["UserName"] = user.UserName; Session["UserId"] = user.Id; return(RedirectToAction("Index", "Home")); } return(RedirectToAction("Index", "Home")); } ViewBag.UserRoleId = db.UserRole.ToList(); // If we got this far, something failed, redisplay form return(View(model)); }