public ActionResult Create(MissingPersonModel collection) { try { string photoUrlToBeAssigned = "~/Content/img/default.jpg"; if (collection.PhotoFile != null) { string pic = Path.GetFileName(collection.PhotoFile.FileName); string newFileName = DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss") + "_" + pic; collection.PhotoFile.SaveAs(Path.Combine(Server.MapPath("~/Content/img"), newFileName)); photoUrlToBeAssigned = "~/Content/img/" + newFileName; } ; var missingPersonToCreate = new MissingPersonModel() { FirstName = collection.FirstName, LastName = collection.LastName, Gender = collection.Gender, PhotoUrl = photoUrlToBeAssigned }; _db.MissingPersons.Add(missingPersonToCreate); _db.SaveChanges(); return(RedirectToAction("Index")); } catch { return(View()); } }
public ActionResult Edit(int id, MissingPersonModel collection) { try { var missingPersonToEdit = _db.MissingPersons.Where(p => p.Id == id).FirstOrDefault(); if (missingPersonToEdit != null) { missingPersonToEdit.FirstName = collection.FirstName; missingPersonToEdit.LastName = collection.LastName; missingPersonToEdit.Gender = collection.Gender; if (collection.PhotoFile != null) { string pic = Path.GetFileName(collection.PhotoFile.FileName); string newFileName = DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss") + "_" + pic; collection.PhotoFile.SaveAs(Path.Combine(Server.MapPath("~/Content/img"), newFileName)); missingPersonToEdit.PhotoUrl = "~/Content/img/" + newFileName; } ; } _db.SaveChanges(); return(RedirectToAction("Details", new { id })); } catch { return(View()); } }