public async Task<ActionResult> Create(FeaturedPropertyViewModel vm, HttpPostedFileBase imageFile) { try { if (ModelState.IsValid) { CloudBlockBlob imageBlob = await _repository.InsertImageBlob(imageFile); if (imageBlob != null) { vm.ImageURL = imageBlob.Uri.ToString(); } //map viewmodel to view var featuredProp = AutoMapperConfig.TCWMapper.Map<FeaturedProperty>(vm); //now save model to db _repository.Insert(featuredProp); await _repository.SaveAsync(); if (imageBlob != null) { _repository.AddMessageToQueue(featuredProp.ID.ToString(), typeof(FeaturedProperty).ToString()); } return RedirectToAction("Index"); } } catch(Exception ex) { //TODO: add logging } return View(vm); }
public async Task<ActionResult> Edit(FeaturedPropertyViewModel vm, HttpPostedFileBase imageFile) { try { if (ModelState.IsValid) { CloudBlockBlob imageBlob = null; if (imageFile != null && imageFile.ContentLength != 0) { // User is changing the image -- delete the existing // image blobs and then upload and save a new one. await _repository.DeleteBlobsAsync(vm.ImageURL, vm.ThumbnailURL); imageBlob = await _repository.InsertImageBlob(imageFile); if (imageBlob != null) { vm.ImageURL = imageBlob.Uri.ToString(); } } //map viewmodel to view var featuredProp = AutoMapperConfig.TCWMapper.Map<FeaturedProperty>(vm); //now save model to db _repository.Update(featuredProp); await _repository.SaveAsync(); if (imageBlob != null) { _repository.AddMessageToQueue(featuredProp.ID.ToString(), typeof(FeaturedProperty).ToString()); } return RedirectToAction("Index"); } } catch (Exception ex) { Console.WriteLine(ex); } return View(vm); }