public async Task<ActionResult> Create(PlaceViewModel model, HttpPostedFileBase ImageUrl) { if (ModelState.IsValid) { var user = UserManager.FindById(User.Identity.GetUserId()); Place placeToCreate = new Place() { CreatorUser = user, Description = model.Description, LocationAddress = model.LocationAddress, Title = model.Title }; db.Places.Add(placeToCreate); try { db.SaveChanges(); } catch (DbEntityValidationException ex) { var a = ex.EntityValidationErrors; } saveImage(ref placeToCreate, ImageUrl); db.SaveChanges(); return RedirectToAction("Details", new { id = placeToCreate.Id }); } return View(model); }
public ActionResult Details(int? id) { if (id == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } Place @place = db.Places.Find(id); if (@place == null) { return HttpNotFound(); } PlaceViewModel placeToShow = new PlaceViewModel() { CreatorUser = @place.CreatorUser, ImageUrl = @place.ImageUrl, LocationAddress = @place.LocationAddress, Title = @place.Title, Description = @place.Description, Id = @place.Id }; return View(placeToShow); }
public ActionResult Edit(PlaceViewModel model, HttpPostedFileBase ImageUrl) { if (ModelState.IsValid) { var user = UserManager.FindById(User.Identity.GetUserId()); Place @place = db.Places.Find(model.Id); @place.CreatorUser = user; @place.Title = model.Title; if (model.ImageUrl != null) { saveImage(ref @place, ImageUrl); } @place.Description = model.Description; try { db.SaveChanges(); } catch (DbEntityValidationException e) { } return RedirectToAction("Details", new { id = @place.Id }); } return View(model); }
public string CheckIfUserCanEditPlace(PlaceViewModel model) { var user = UserManager.FindById(User.Identity.GetUserId()); if (user.Id == model.CreatorUser.Id) { return string.Format("<a href=\"/Place/Edit?id=" + model.Id + "\">Edit place<a/>"); } else { return ""; } }
public ActionResult Edit(int? id) { if (id == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } Place @place = db.Places.Find(id); if (@place == null) { return HttpNotFound(); } PlaceViewModel model = new PlaceViewModel() { Id = @place.Id, CreatorUser = @place.CreatorUser, Description = @place.Description, ImageUrl = @place.ImageUrl, Title = @place.Title }; return View(model); }