public ActionResult Create([Bind(Include = "Id,nombre,fechaNac,caracteristicas,tamanioId,razaId,enAdopcion")] Animal animal, HttpPostedFileBase upload) { animal.fechaAlta = DateTime.Now; animal.edad = DateTime.Now.Year - animal.fechaNac.Year; try { if (ModelState.IsValid) //Verifica que el formulario sea valido { if (upload != null && upload.ContentLength > 0) //verifica se cargo una foto para en animal { var avatar = new AERHiPets.Models.GestionAnimal.GestionAnimalImagenes.File //instancia la clase encargada de mapear la foto en la BD { FileName = System.IO.Path.GetFileName(upload.FileName), FileType = FileType.Avatar, ContentType = upload.ContentType }; using (var reader = new System.IO.BinaryReader(upload.InputStream))//transforma la foto en una cadena de bytes para guardar en la BD { avatar.Content = reader.ReadBytes(upload.ContentLength); } animal.Files = new List <AERHiPets.Models.GestionAnimal.GestionAnimalImagenes.File> { avatar }; //aniade la referencia de la foto guardada al atributo correspondiente en animal } db.Animales.Add(animal); db.SaveChanges(); return(RedirectToAction("Index")); } } catch (RetryLimitExceededException /* dex */) { //Log the error (uncomment dex variable name and add a line here to write a log. ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists see your system administrator."); } ViewBag.razaId = new SelectList(db.Razas, "Id", "nombre", animal.razaId); ViewBag.tamanioId = new SelectList(db.Tamanios.Where(a => a.fechaBaja == null), "Id", "nombre", animal.tamanioId); return(View(animal)); }
public ActionResult Create([Bind(Include = "Id,nombre,fechaNac,caracteristicas,tamanioId,razaId,enAdopcion")] Animal animal, HttpPostedFileBase upload) { animal.fechaAlta = DateTime.Now; animal.edad = DateTime.Now.Year - animal.fechaNac.Year; try { if (ModelState.IsValid)//Verifica que el formulario sea valido { if (upload != null && upload.ContentLength > 0)//verifica se cargo una foto para en animal { var avatar = new AERHiPets.Models.GestionAnimal.GestionAnimalImagenes.File//instancia la clase encargada de mapear la foto en la BD { FileName = System.IO.Path.GetFileName(upload.FileName), FileType = FileType.Avatar, ContentType = upload.ContentType }; using (var reader = new System.IO.BinaryReader(upload.InputStream))//transforma la foto en una cadena de bytes para guardar en la BD { avatar.Content = reader.ReadBytes(upload.ContentLength); } animal.Files = new List<AERHiPets.Models.GestionAnimal.GestionAnimalImagenes.File> { avatar };//aniade la referencia de la foto guardada al atributo correspondiente en animal } db.Animales.Add(animal); db.SaveChanges(); return RedirectToAction("Index"); } } catch (RetryLimitExceededException /* dex */) { //Log the error (uncomment dex variable name and add a line here to write a log. ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists see your system administrator."); } ViewBag.razaId = new SelectList(db.Razas, "Id", "nombre", animal.razaId); ViewBag.tamanioId = new SelectList(db.Tamanios.Where(a => a.fechaBaja == null), "Id", "nombre", animal.tamanioId); return View(animal); }