public ActionResult Create(FormCollection collection) { try { _dbContext = new ApplicationDbContext(); UserAdService userAdService = new UserAdService(_dbContext); var manager = new UserManager <ApplicationUser>(new UserStore <ApplicationUser>(_dbContext)); userAdService.CreateAd(new UserAdVM { CreatedOn = DateTime.UtcNow, UserId = User.Identity.GetUserName() }); return(RedirectToAction("Create")); } catch { return(View()); } }
public ActionResult UploadAdImage(HttpPostedFileBase adImage, string returnUrl, int adImageNumber, int adID, string SubmitAction = "") { string newAdImageFilename = ""; //int adID = 0; if (adImage != null) { if (adImage.ContentLength > 0) { _dbContext = new ApplicationDbContext(); var userAdService = new UserAdService(_dbContext); //Save an Ad first and get newly created Ad's ID if (adID == 0) { var userAdVM = new UserAdVM { UserId = User.Identity.GetUserId(), CreatedOn = DateTime.UtcNow, IsDraft = true }; adID = userAdService.CreateAd(userAdVM); } // UnComment After making it dynamic var userAdImage = userAdService.GetUserAdImages(adID).Where(w => w.ImageNumber == adImageNumber).FirstOrDefault(); //Save New Ad Image and Remove Old Image string adImageName = DateTime.UtcNow.ToString("yyyyddmmhhmmss") + "_ad_original.jpg"; string adImagePath = Server.MapPath("~" + _FileSavingPaths.UserAdsPath + "/" + adImageName); //Save New Image if (!System.IO.File.Exists(adImagePath)) { adImage.SaveAs(adImagePath); } //Remove Old Image // UnComment After making it dynamic if (userAdImage != null) { try { //Delete Original and Selected Images if (System.IO.File.Exists(Server.MapPath("~" + _FileSavingPaths.UserAdsPath + "/" + userAdImage.AdImageOriginal))) { System.IO.File.Delete(Server.MapPath("~" + _FileSavingPaths.UserAdsPath + "/" + userAdImage.AdImageOriginal)); } if (System.IO.File.Exists(Server.MapPath("~" + _FileSavingPaths.UserAdsPath + "/" + userAdImage.AdImageSelected))) { System.IO.File.Delete(Server.MapPath("~" + _FileSavingPaths.UserAdsPath + "/" + userAdImage.AdImageSelected)); } } catch { } } //Add user Ad image in database var userAdImageVM = new UserAdImageVM { AdImageOriginal = adImageName, AdImageSelected = adImageName, ImageNumber = adImageNumber, CreatedOn = DateTime.UtcNow, AdID = adID }; var uploadedImageId = userAdService.AddUserAdImage(userAdImageVM); newAdImageFilename = adImageName; //// Increment the image number after saving the previous image //ViewBag. //userProfile.ProfileImageOriginal = profileImageName; //userProfile.ProfileImageSelected = profileImageName; //newProfileImageFilename = profileImageName; //userAccountService.UpdateUserProfileImage(userProfile, false); //Set the Image Uploaded TempData["ImageUploaded"] = true; TempData["ImageUploadedId"] = uploadedImageId; TempData["ResponseResult"] = new ResponseResult <object> { Success = true, Message = "Ad Image is uploaded successfully.", MessageCode = ResponseResultMessageCode.ProfileImageUploaded, }; } } if (SubmitAction.ToLower().Equals(Strings.Next.ToLower()) && adID != 0) { // store the adId in tempData //TempData["AdId"] = adID; return(RedirectToAction("AdWizardStep2", "Ad", new { adID })); } if (Request.IsAjaxRequest()) { return(Json(new ResponseResult <string> { Success = true, Data = newAdImageFilename }, JsonRequestBehavior.AllowGet)); } else { if (!string.IsNullOrEmpty(returnUrl)) { if (adImage == null) { TempData["ResponseResult"] = new ResponseResult <object> { Success = false, Message = "Please upload atleast one image to continue to the next step.", MessageCode = ResponseResultMessageCode.ProfileImageUploaded, }; return(Redirect(returnUrl)); } return(RedirectToAction("AdWizardStep1", "Ad", new { adID })); } else { return(RedirectToAction("Index")); } } }