public ActionResult Create(CreateForSaleViewModel forsale) { if (ModelState.IsValid) { //Set username on seller forsale.Username = User.Identity.Name; forsale.Seller.Username = User.Identity.Name; if (forsale.Picture != null && forsale.Picture.ContentLength > 0) { var extension = Path.GetExtension(forsale.Picture.FileName).ToLowerInvariant().Substring(1); if (!ConfigurationManager.AppSettings["AllowedImageExtensions"].Contains(extension)) { ModelState.AddModelError("Picture", "File type is not allowed. (jpg, jpeg, png and gif) "); return View(forsale); } } var me = new ForSale { Username = forsale.Username, LiteratureReferenceForSale = forsale.LiteratureReferenceForSale, Price = forsale.Price, Title = forsale.Title, Trading = forsale.Trading, When = DateTime.Now, Description = forsale.Description, Seller = forsale.Seller, }; db.ForSale.Add(me); db.SaveChanges(); if (forsale.Picture != null && forsale.Picture.ContentLength > 0) { var extension = Path.GetExtension(forsale.Picture.FileName).ToLowerInvariant(); var filename = String.Concat(me.Id, "-ForSalePicture", extension); var path = Path.Combine(Server.MapPath("~/Uploads/ForSale"), filename); forsale.Picture.SaveAs(path); me.PicturePath = String.Concat("/Uploads/ForSale/", filename); db.Entry(me).State = EntityState.Modified; } if (db.Entry(me).State == EntityState.Modified) { db.SaveChanges(); } return RedirectToAction("Index"); } return View(forsale); }
public ActionResult CreateWithUserInformation(CreateForSaleViewModel forsale) { if (ModelState.IsValid) { //Set username on seller forsale.Seller.Username = User.Identity.Name; forsale.Username = User.Identity.Name; if (forsale.Picture != null && forsale.Picture.ContentLength > 0) { var extension = Path.GetExtension(forsale.Picture.FileName).ToLowerInvariant().Substring(1); if (!ConfigurationManager.AppSettings["AllowedImageExtensions"].Contains(extension)) { ModelState.AddModelError("Picture", "File type is not allowed. (jpg, jpeg, png and gif) "); return View(forsale); } } var ControllIfUserExists = db.Profiles.Where(f => f.Username == forsale.Username).Select(f => f); if (ControllIfUserExists.Count() > 0) { string Username = User.Identity.Name; var query = from a in db.Profiles where a.Username == Username select a; foreach (Profile a in query) { if (a.Firstname != null){ forsale.Seller.Firstname = a.Firstname; }else{ forsale.Seller.Firstname = User.Identity.Name; } if (a.Email != null){ forsale.Seller.Email = a.Email; } forsale.Seller.Lastname = a.Lastname; forsale.Seller.Phone = a.Phone; forsale.Seller.Username = a.Username; } } else { forsale.Seller.Firstname = User.Identity.Name; } var me = new ForSale { Username = forsale.Username, LiteratureReferenceForSale = forsale.LiteratureReferenceForSale, Price = forsale.Price, Title = forsale.Title, Trading = forsale.Trading, When = DateTime.Now, Description = forsale.Description, Seller = forsale.Seller, }; db.ForSale.Add(me); db.SaveChanges(); if (forsale.Picture != null && forsale.Picture.ContentLength > 0) { var extension = Path.GetExtension(forsale.Picture.FileName).ToLowerInvariant(); var filename = String.Concat(me.Id, "-ForSalePicture", extension); var path = Path.Combine(Server.MapPath("~/Uploads/ForSale"), filename); forsale.Picture.SaveAs(path); me.PicturePath = String.Concat("/Uploads/ForSale/", filename); db.Entry(me).State = EntityState.Modified; } if (db.Entry(me).State == EntityState.Modified) { db.SaveChanges(); } return RedirectToAction("Index"); } return View(forsale); }