public ActionResult CreateByUrl(LocationImageUrlViewModel model) { // TODO finish // List of allowed image types (for hosting on web) var validImageTypes = new string[] { ".gif", ".jpg", ".jpeg", ".pjpeg", // needed for compatability with some older jpegs ".png" }; // 1. check if a valid image was uploaded if (model.ImageUrl == null || model.ImageUrl.Length == 0) { System.Diagnostics.Debug.WriteLine("Image is null or empty"); ModelState.AddModelError("ImageUpload", "This field is required"); } // 2. check that image is of a valid filetype else { bool hasValidExtension = false; foreach (String s in validImageTypes) { if (model.ImageUrl.EndsWith(s)) { hasValidExtension = true; break; } } if (!hasValidExtension) { System.Diagnostics.Debug.WriteLine("Invalid Image Type"); ModelState.AddModelError("ImageUpload", "Please choose either a GIF, JPG or PNG image."); } } // 3. create the Image entry to save to the database if (ModelState.IsValid) { System.Diagnostics.Debug.WriteLine("Model state is valid"); // initialize image model to store in database var locationImage = new LocationImage { LocationID = model.LocationID, Title = model.Title, AltText = "Image from " + db.Locations.Find(model.LocationID).Label, DateTaken = model.DateTaken, DateCreated = DateTime.UtcNow, DateModified = DateTime.UtcNow, ImageUrl = model.ImageUrl, User = manager.FindById(User.Identity.GetUserId()) }; // save LocationImage model in database db.LocationImages.Add(locationImage); db.SaveChanges(); return RedirectToAction("Media", "Location", new { locationID = model.LocationID }); } // otherwise, go back to create view. System.Diagnostics.Debug.WriteLine("Model state is invalid."); ViewBag.LocationID = model.LocationID; ViewBag.LocationLabel = db.Locations.Find(model.LocationID).Label; ViewBag.PlaceholderUrl = "~/Content/ImagePreview.png"; ViewBag.LocationCount = db.Locations.Count(); return View(model); }
public ActionResult CreateByUrl(int locationID) { // updated to include notes from Monday's meeting ViewBag.PlaceholderUrl = "~/Content/ImagePreview.png"; ViewBag.LocationLabel = db.Locations.Find(locationID).Label; LocationImageUrlViewModel viewModel = new LocationImageUrlViewModel(); viewModel.LocationID = locationID; return View(viewModel); }