public ActionResult Add(BeerRequestViewModel model, HttpPostedFileBase upload) { if (!this.ModelState.IsValid) { return this.View(model); } var beer = this.Mapper.Map<Beer>(model); if (upload != null && upload.ContentLength > 0 && upload.ContentType == "image/jpeg") { string id = beer.Id.ToString(); string directory = this.Server.MapPath("~/UploadedFiles/BeersImages/") + id; if (!Directory.Exists(directory)) { Directory.CreateDirectory(directory); } string filename = Guid.NewGuid().ToString() + ".jpg"; string path = directory + "/" + filename; string url = "~/UploadedFiles/BeersImages/" + id + "/" + filename; upload.SaveAs(path); beer.PhotoUrl = url; } else { beer.PhotoUrl = "~/UploadedFiles/beer-avatar.jpg"; } var beerId = this.beers.Add(beer); return this.RedirectToAction("Details", new { id = this.identifier.EncodeId(beerId) }); }
public ActionResult Add(BeerRequestViewModel model, HttpPostedFileBase upload) { if (!this.ModelState.IsValid) { return(this.View(model)); } var beer = this.Mapper.Map <Beer>(model); if (upload != null && upload.ContentLength > 0 && upload.ContentType == "image/jpeg") { string id = beer.Id.ToString(); string directory = this.Server.MapPath("~/UploadedFiles/BeersImages/") + id; if (!Directory.Exists(directory)) { Directory.CreateDirectory(directory); } string filename = Guid.NewGuid().ToString() + ".jpg"; string path = directory + "/" + filename; string url = "~/UploadedFiles/BeersImages/" + id + "/" + filename; upload.SaveAs(path); beer.PhotoUrl = url; } else { beer.PhotoUrl = "~/UploadedFiles/beer-avatar.jpg"; } var beerId = this.beers.Add(beer); return(this.RedirectToAction("Details", new { id = this.identifier.EncodeId(beerId) })); }
public ActionResult Add() { var types = this.beerTypes.GetAll().OrderBy(x => x.Name).ToArray(); var beerCountries = this.countries.GetAll().OrderBy(x => x.Name).ToArray(); var viewTypes = this.Mapper.Map<IEnumerable<SimpleBeerTypeResponseViewModel>>(types); var viewCountries = this.Mapper.Map<IEnumerable<SimpleCountryResponseViewModel>>(beerCountries); var model = new BeerRequestViewModel(); model.BeerTypes = viewTypes; model.Countries = viewCountries; return this.View(model); }
public ActionResult Add() { var types = this.beerTypes.GetAll().OrderBy(x => x.Name).ToArray(); var beerCountries = this.countries.GetAll().OrderBy(x => x.Name).ToArray(); var viewTypes = this.Mapper.Map <IEnumerable <SimpleBeerTypeResponseViewModel> >(types); var viewCountries = this.Mapper.Map <IEnumerable <SimpleCountryResponseViewModel> >(beerCountries); var model = new BeerRequestViewModel(); model.BeerTypes = viewTypes; model.Countries = viewCountries; return(this.View(model)); }