public ActionResult CreateAccountInfo(AccountInfoCreate model) { var service = CreateAccountInfoService(); var validImageTypes = new string[] { "image/gif", "image/jpeg", "image/png" }; if (model.PictureImage != null && !validImageTypes.Contains(model.PictureImage.ContentType)) { ModelState.AddModelError("ImageUpload", "Please choose either a GIF, JPG or PNG image."); } if (!ModelState.IsValid) // This needs to be if not { return View(model); } if (service.CreateAccountInfo(model)) { TempData["SaveResult"] = "Your Account info was created!"; return RedirectToAction("Index"); } ModelState.AddModelError("", "Account Info could not be created."); return View(model); }
public string UploadedFile(AccountInfoCreate model) // creates the unique URL and loads the image into the file folder { string uniqueFileName = ""; if (model.PictureImage != null) { string uploadsFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Content/Images"); uniqueFileName = Guid.NewGuid().ToString() + "-" + model.PictureImage.FileName; string filePath = Path.Combine(uploadsFolder, uniqueFileName); model.PictureImage.SaveAs(filePath); } else if (model.PictureImage == null) { return(uniqueFileName = "default-avatar-image.jpg"); } return(uniqueFileName); }
public bool CreateAccountInfo(AccountInfoCreate model) { string uniqueFileName = UploadedFile(model); var entity = new AccountInfo() { UserID = _userID, FirstName = model.FirstName, LastName = model.LastName, State = model.State, Country = model.Country, PictureURL = uniqueFileName, CreateUTC = DateTimeOffset.Now }; using (var ctx = new ApplicationDbContext()) { ctx.Accounts.Add(entity); return(ctx.SaveChanges() == 1); } }