[Authorize] // GET: /Listings/Add public ActionResult Add() { var model = new ListingAddViewModel(); model.Listing = new Listing(); PopulateListingAddDropDowns(model); return(View(model)); }
private void PopulateListingAddDropDowns(ListingAddViewModel 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 ActionResult Add() { var model = new ListingAddViewModel(); var statesRepo = StatesRepositoryFactory.GetRepository(); var bathroomRepo = BathroomTypesRepositoryFactory.GetRepository(); model.States = new SelectList(statesRepo.GetAll(), "StateId", "StateId"); model.BathroomTypes = new SelectList(bathroomRepo.GetAll(), "BathroomTypeId", "BathroomTypeName"); model.Listing = new Listing(); return(View(model)); }
public ActionResult Add(ListingAddViewModel model) { if (ModelState.IsValid) { var repo = ListingRepositoryFactory.GetRepository(); try { model.Listing.UserId = AuthorizeUtilities.GetUserId(this); 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); } repo.Insert(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)); } }
[Authorize] // POST: /Listings/Add public ActionResult Add(ListingAddViewModel model) { if (ModelState.IsValid) { try { //var repo = ListingRepositoryFactory.GetRepository(); model.Listing.UserId = Util.GetUserId(this); 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); } //repo.ListingInsert(model.Listing); _svc.ListingInsert(model.Listing); return(RedirectToAction("Edit", new { id = model.Listing.ListingId })); } catch (Exception ex) { throw ex; } } else { PopulateListingAddDropDowns(model); return(View(model)); } }