// GET: People/Create public IActionResult AgregarHistorial(int id) { var persona = _context.Persons.Where(x => x.PersonID == id).FirstOrDefault(); if (persona == null) { return(null); } var ch = new AddCreditHistory { PersonID = persona.PersonID, FirstName = persona.FirstName, LastName = persona.LastName, Identification = persona.Identification }; return(View(ch)); }
public async Task <IActionResult> AgregarHistorial(AddCreditHistory c) { try { if (ModelState.IsValid) { if (c.Files.Count() <= 0) { ModelState.AddModelError("", "Favor seleccionar uno o mas archivos de evidencia"); return(View(c)); } var user = _userManager.GetUserName(User); var persona = _context.Persons.Where(x => x.PersonID == c.PersonID).FirstOrDefault(); if (persona == null) { return(View(c)); } var credit = new CreditHistory { Person = persona, CreateDate = DateTime.Now, Note = c.Description, CreateBy = user }; var credits = new List <CreditHistory> { credit }; //save images var FullfilePath = System.IO.Directory.GetCurrentDirectory(); var relativePath = @"\images\Upload\Evidencia_"; foreach (var formFile in c.Files) { if (formFile.Length > 0) { string strFileExtension = System.IO.Path.GetExtension(formFile.FileName); var relativePath1 = relativePath + $"{Guid.NewGuid()}{strFileExtension}"; var FullfilePath1 = $"{FullfilePath}{relativePath1}"; using (var stream = new FileStream(FullfilePath1, FileMode.Create)) { await formFile.CopyToAsync(stream); } var picture = new Picture { PicturePath = relativePath1, CreditHistory = credit }; _context.Add(picture); } } persona.AddCreditHistory(credits); _context.Update(persona); await _context.SaveChangesAsync(); ModelState.Clear(); c.Resultados = "El historial hasido creado."; c.Description = ""; //return RedirectToAction("Index"); c.FirstName = persona.FirstName; c.LastName = persona.LastName; c.PersonID = persona.PersonID; c.Identification = persona.Identification; } } catch (DbUpdateException ex) { //Log the error (uncomment ex variable name and write a log. ModelState.AddModelError("", ex.Message + " Unable to save changes. " + "Try again, and if the problem persists " + "see your system administrator."); } return(View(c)); }