public ActionResult AddPhoto(Photos photo, HttpPostedFileBase image) { string json; ModelState.Clear(); if (TryValidateModel(photo)) { var id = _photoRepository.AddPhoto(photo, image.ContentType, new byte[image.ContentLength], image); json = JsonConvert.SerializeObject(new { id = id, success = true, message = "Post added successfully." }); } else { json = JsonConvert.SerializeObject(new { id = 0, success = false, message = "Failed to add the post." }); } return(RedirectToAction("AllAlbums", "AdminPhoto")); }
public ActionResult addPhoto(HttpPostedFileBase useralbumphoto_photo, PhotoModel model) { if (ModelState.IsValid) { if (useralbumphoto_photo.ContentLength > 0) { string path = Server.MapPath("~/photo"); string filename = Path.GetFileName(useralbumphoto_photo.FileName); string fullpath = Path.Combine(path, filename); string imagepath = Path.Combine("/photo/", filename); try { useralbumphoto_photo.SaveAs(fullpath); } catch (Exception ex) { return(Content(ex.Message)); } model.useralbumphoto_photo = imagepath; int i = photorepo.AddPhoto(model); if (i >= 1) { return(RedirectToAction("Index")); } else { ViewBag.msg = "Failed"; return(View()); } } else { ViewBag.msg = "Failed"; return(View()); } } else { ViewBag.msg = "Failed"; return(View()); } }
public async Task <IActionResult> Create(PhotoCreateViewModel model) { if (ModelState.IsValid) { if (model.Image == null) { _clientNotification.AddToastNotification("You Have to Upload An Image!", NotificationHelper.NotificationType.error, new ToastNotificationOption { NewestOnTop = true, CloseButton = true, PositionClass = "toast-top-right", PreventDuplicates = true }); return(View(model)); } if (model.Image.Length > _options.MaxBytes) { _clientNotification.AddToastNotification("Image File size Exceeded", NotificationHelper.NotificationType.error, new ToastNotificationOption { NewestOnTop = true, CloseButton = true, PositionClass = "toast-top-right", PreventDuplicates = true }); return(View(model)); } if (!_options.IsSupported(model.Image.FileName)) { _clientNotification.AddToastNotification("Invalid File Type", NotificationHelper.NotificationType.error, new ToastNotificationOption { NewestOnTop = true, CloseButton = true, PositionClass = "toast-top-right", PreventDuplicates = true }); } else { var photo = new Photo { Id = Guid.NewGuid().ToString().Replace("-", string.Empty), Name = model.Name, Description = model.Description, Category = model.Category, PhotoUrl = _fileManager.SaveImage(model.Image), FaceBookLink = model.FacebookLink, InstagramLink = model.InstagramLink, TwitterLink = model.TwitterLink }; await _photoRepo.AddPhoto(photo); await _photoRepo.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } } _clientNotification.AddToastNotification("You Have Errors", NotificationHelper.NotificationType.error, new ToastNotificationOption { NewestOnTop = true, CloseButton = true, PositionClass = "toast-top-right", PreventDuplicates = true }); return(View(model)); }