public ActionResult Nuevo(SliderFormModel Form) { if (Request.Files.Count > 0 && Form.Imagen != null) { HttpPostedFileBase file = this.Request.Files[0]; try { string uri = ProcesarBlob(file); Slider aux = new Slider() { FechaIngreso = DateTime.Now, Imagen = uri, Titulo = Form.Titulo, Usuario = User.Identity.Name, Id = Guid.NewGuid() }; db.Slider.Add(aux); db.SaveChanges(); } catch { Form.Mensaje = "Error"; } } else { Form.Mensaje = "Es obligatorio subir una fotografia"; } return(RedirectToAction("Index")); }
public ActionResult Modificar(SliderFormModel Form) { if (Request.Files.Count > 0 && Form.Imagen != null) { HttpPostedFileBase file = this.Request.Files[0]; Slider aux = db.Slider.First(x => x.Id == Form.Id); Uri uri = new Uri(aux.Imagen); string filename = Path.GetFileName(uri.LocalPath); var blob = ObtenerConexionAzureStorage().GetBlockBlobReference(filename); blob.DeleteIfExists(); try { string nuevoUri = ProcesarBlob(file); aux.Titulo = Form.Titulo; aux.Imagen = nuevoUri; aux.Usuario = User.Identity.Name; aux.FechaIngreso = DateTime.Now; db.Entry(aux).State = EntityState.Modified; db.SaveChanges(); } catch { Form.Mensaje = "Error"; } db.SaveChanges(); } else { Form.Mensaje = "Es obligatorio subir una fotografia"; } return(RedirectToAction("Index")); }
public IActionResult OperateSlider(SliderFormModel model, IFormFile image) { if (ModelState.IsValid) { if (model.Id == 0 || model.Id == null) { try { if (image != null) { string imageBase64Data = ImageOperations.GetBase64FromFile(image); model.Image = imageBase64Data; Slider slider = new Slider { Image = model.Image, ScreenOrder = model.ScreenOrder }; db.Add(slider); db.SaveChanges(); return(StatusCode(200, "Eklendi")); } else { ModelState.AddModelError("error", "Lütfen Resim Ekleyin!"); } } catch (Exception) { ModelState.AddModelError("error", "Error! An error occurred while Slider creating"); } } else { var Slider = db.Slider.Where(q => q.Id == model.Id).FirstOrDefault(); if (Slider == null) { ModelState.AddModelError("error", "Unknown Request!"); } else { if (image != null) { string imageBase64Data = ImageOperations.GetBase64FromFile(image); Slider.Image = imageBase64Data; } Slider.ScreenOrder = model.ScreenOrder; db.Update(Slider); db.SaveChanges(); return(StatusCode(200, "Güncellendi!")); } } } return(BadRequest(new { Message = "Some error(s) occurred!", StatusCode = 400, ModelState = ModelState.ToList() })); }