public ActionResult Create([Bind(Include = "Id,Description,TotalArea,FloorArea,Age,Rooms,Bathrooms,Price,Status,TypeOwnerShips_Id,Operations_Id,Provinces_Id,Cities_Id,District_Id,Location,Ref,UrlMaps,Order,StatusOwnerShips_Id")] OwnerShip ownership, HttpPostedFileBase[] files) { if (files[0] == null) { ModelState.AddModelError("", "Seleccione una imagen."); this.LoadViewBag(ownership); return View(ownership); } Boolean validatesizeimage = false; foreach (HttpPostedFileBase file in files) { if (file.ContentLength > 1048576) { validatesizeimage = true; } } if (validatesizeimage) { ModelState.AddModelError("", "Seleccionar imagenes de menor tamaño"); this.LoadViewBag(ownership); return View(ownership); } if (ModelState.IsValid) { if (ownership.Order != null) { var value = db.OwnerShips.Where(x => x.Order == ownership.Order).FirstOrDefault(); if (value != null) { ownership.Order = null; } } foreach (HttpPostedFileBase item in files) { var picture = new OwnerShipPicture(); string imageName = System.IO.Path.GetFileName(item.FileName); string physicalPath = Server.MapPath("~/Image/" + imageName); item.SaveAs(physicalPath); picture.OwnerShip_Id = ownership.Id; picture.Picture = imageName; ownership.OwnerShipPictures.Add(picture); } db.OwnerShips.Add(ownership); db.SaveChanges(); return RedirectToAction("Index"); } this.LoadViewBag(ownership); return View(ownership); }
public ActionResult SaveImages(HttpPostedFileBase[] myupload, int? Id) { if (myupload != null && myupload[0] != null) { Boolean validatesizeimage = false; foreach (HttpPostedFileBase file in myupload) { if (file.ContentLength > 1048576) { validatesizeimage = true; } } if (validatesizeimage) { return RedirectToAction("Edit", new { id = Id }); } OwnerShip ownerships = db.OwnerShips.Include(o => o.OwnerShipPictures).Where(x => x.Id == Id.Value).FirstOrDefault(); db.OwnerShipPicture.RemoveRange(ownerships.OwnerShipPictures); db.SaveChanges(); OwnerShipPicture picture; foreach (HttpPostedFileBase item in myupload) { if (item != null) { picture = new OwnerShipPicture(); string imageName = System.IO.Path.GetFileName(item.FileName); string physicalPath = Server.MapPath("~/Image/" + imageName); item.SaveAs(physicalPath); picture.OwnerShip_Id = Id.Value; picture.Picture = imageName; db.OwnerShipPicture.Add(picture); db.SaveChanges(); } } } return RedirectToAction("Edit", new { id = Id }); }