public IActionResult EditListing(ListingEditViewModel model) { if (ModelState.IsValid) { Listing listing = this._listingRepository.GetListing(model.ListingId); listing.Address = model.Address; listing.Company = model.Company; listing.Price = model.Price; if (model.Image != null) { if (model.ExitingImagePath != null) { string imagePath = Path.Combine(this._hostingEnvironment.WebRootPath, "/img/thumb", model.ExitingImagePath); System.IO.File.Delete(imagePath); } var uniqueFileName = ProcessUploadFile(model); listing.Image = uniqueFileName; } this._listingRepository.Update(listing); return(RedirectToAction("index")); } return(View()); }
[Authorize] // POST: /Listings/Edit public ActionResult Edit(ListingEditViewModel model) { if (ModelState.IsValid) { try { //var repo = ListingRepositoryFactory.GetRepository(); model.Listing.UserId = Util.GetUserId(this); //var oldListing = repo.ListingGetById(model.Listing.ListingId); var oldListing = _svc.ListingGetById(model.Listing.ListingId); if (model.ImageUpload != null && model.ImageUpload.ContentLength > 0) { string savePath = Server.MapPath("~/Images"); string fileName = Path.GetFileNameWithoutExtension(model.ImageUpload.FileName); string extension = Path.GetExtension(model.ImageUpload.FileName); string filePath = Path.Combine(savePath, fileName + extension); int counter = 1; while (System.IO.File.Exists(filePath)) { filePath = Path.Combine(savePath, fileName + counter.ToString() + extension); counter++; } model.ImageUpload.SaveAs(filePath); model.Listing.ImageFileName = Path.GetFileName(filePath); var oldPath = Path.Combine(savePath, oldListing.ImageFileName); if (System.IO.File.Exists(oldPath)) { System.IO.File.Delete(oldPath); } } else { // they did not replace the old file, so keep the old file name model.Listing.ImageFileName = oldListing.ImageFileName; } //repo.ListingUpdate(model.Listing); _svc.ListingUpdate(model.Listing); return(RedirectToAction("Edit", new { id = model.Listing.ListingId })); } catch (Exception ex) { throw ex; } } else { PopulateListingEditDropDowns(model); return(View(model)); } }
private void PopulateListingEditDropDowns(ListingEditViewModel model) { /* * var statesRepo = StatesRepositoryFactory.GetRepository(); * var bathroomTypesRepo = BathroomTypesRepositoryFactory.GetRepository(); * model.States = new SelectList(statesRepo.StateGetAll(), "StateId", "StateId"); * model.BathroomTypes = new SelectList(bathRoomTypesRepo.BathroomTypeGetAll(), "BathroomTypeId", "BathroomTypeName"); */ model.States = new SelectList(_svc.StateGetAll(), "StateId", "StateId"); model.BathroomTypes = new SelectList(_svc.BathroomTypeGetAll(), "BathroomTypeId", "BathroomTypeName"); }
public ViewResult EditListing(int id) { Listing listing = this._listingRepository.GetListing(id); ListingEditViewModel listingEditViewModel = new ListingEditViewModel { ListingId = listing.ListingId, Address = listing.Address, Company = listing.Company, ExitingImagePath = listing.Image, Price = listing.Price }; ViewData["listingId"] = id; return(View(listingEditViewModel)); }
[Authorize] // GET: /Listings/Edit public ActionResult Edit(int id) { //var repo = ListingRepositoryFactory.GetRepository(); var model = new ListingEditViewModel(); //model.Listing = repo.ListingGetBy(id); model.Listing = _svc.ListingGetById(id); if (model.Listing.UserId != Util.GetUserId(this)) { throw new Exception("Attempt to edit a listing you do not own! Shame!"); } PopulateListingEditDropDowns(model); return(View(model)); }
public ActionResult Edit(int id) { var model = new ListingEditViewModel(); var statesRepo = StatesRepositoryFactory.GetRepository(); var bathroomRepo = BathroomTypesRepositoryFactory.GetRepository(); var listingsRepo = ListingRepositoryFactory.GetRepository(); model.States = new SelectList(statesRepo.GetAll(), "StateId", "StateId"); model.BathroomTypes = new SelectList(bathroomRepo.GetAll(), "BathroomTypeId", "BathroomTypeName"); model.Listing = listingsRepo.GetById(id); if (model.Listing.UserId != AuthorizeUtilities.GetUserId(this)) { throw new Exception("Attempt to edit a listing you do not own! Naughty!"); } return(View(model)); }
public ActionResult Edit(ListingEditViewModel model) { if (ModelState.IsValid) { var repo = ListingRepositoryFactory.GetRepository(); try { model.Listing.UserId = AuthorizeUtilities.GetUserId(this); var oldListing = repo.GetById(model.Listing.ListingId); if (model.ImageUpload != null && model.ImageUpload.ContentLength > 0) { var savepath = Server.MapPath("~/Images"); string fileName = Path.GetFileNameWithoutExtension(model.ImageUpload.FileName); string extension = Path.GetExtension(model.ImageUpload.FileName); var filePath = Path.Combine(savepath, fileName + extension); int counter = 1; while (System.IO.File.Exists(filePath)) { filePath = Path.Combine(savepath, fileName + counter.ToString() + extension); counter++; } model.ImageUpload.SaveAs(filePath); model.Listing.ImageFileName = Path.GetFileName(filePath); // delete old file var oldPath = Path.Combine(savepath, oldListing.ImageFileName); if (System.IO.File.Exists(oldPath)) { System.IO.File.Delete(oldPath); } } else { // they did not replace the old file, so keep the old file name model.Listing.ImageFileName = oldListing.ImageFileName; } repo.Update(model.Listing); return(RedirectToAction("Edit", new { id = model.Listing.ListingId })); } catch (Exception ex) { throw ex; } } else { var statesRepo = StatesRepositoryFactory.GetRepository(); var bathroomRepo = BathroomTypesRepositoryFactory.GetRepository(); model.States = new SelectList(statesRepo.GetAll(), "StateId", "StateId"); model.BathroomTypes = new SelectList(bathroomRepo.GetAll(), "BathroomTypeId", "BathroomTypeName"); return(View(model)); } }