public async Task <ActionResult> Create([Bind(Include = "Id,user1,email,password,confirmPassword,nombreFichero,foto")] User user, HttpPostedFileBase upload) { try { if (ModelState.IsValid) { //Para resolver más fácil valorDuplicado(e), lo almacenamos sin espacios. user.user1 = user.user1.Trim(); user.email = user.email.Trim(); user.password = user.password.Trim(); // if (upload != null && upload.ContentLength > 0) { Debug.WriteLine(upload.FileName); if (user.nombreFichero == null) { user.nombreFichero = upload.FileName; } var imagen = new Models.File { FileName = System.IO.Path.GetFileName(upload.FileName), FileType = (int?)FileType.Image, ContentType = upload.ContentType }; using (var reader = new System.IO.BinaryReader(upload.InputStream)) { imagen.Content = reader.ReadBytes(upload.ContentLength); user.foto = imagen.Content; } user.Files = new List <Models.File> { imagen }; user = db.Users.Add(user); } else { Debug.WriteLine("No ha upload"); user = db.Users.Add(user); } await db.SaveChangesAsync(); return(RedirectToAction("Index", "Home", new { id = 0 })); } } catch (RetryLimitExceededException /* dex */) { //Log the error (uncomment dex variable name and add a line here to write a log. ModelState.AddModelError("No ha sido posible añadir el usuario", " Inténtalo de nuevo, si el problema persisite contacte con el administrador."); } catch (System.Data.Entity.Infrastructure.DbUpdateException e) { ViewBag.ErrorMessage = "Ya existe " + valorDuplicado(e); } ViewBag.UserId = new SelectList(db.Users, "Id", "user1", user.Id); return(View(user)); }
public async Task <ActionResult> Create([Bind(Include = "Id,descripcion,fecha,nombreFichero,foto,UserId")] Coch coch) { if (ModelState.IsValid) { db.Coches.Add(coch); await db.SaveChangesAsync(); return(RedirectToAction("Index")); } ViewBag.UserId = new SelectList(db.Users, "Id", "user1", coch.UserId); return(View(coch)); }