public IActionResult Edit(PersonVM model, int[] dutyarray) { Person person = _bookcontext.People.Include(q => q.PersonDuties).FirstOrDefault(q => q.ID == model.PersonID); if (ModelState.IsValid) { person.Name = model.Name; person.SurName = model.SurName; person.Biography = model.Biography; person.BirthDate = model.BirthDate; if (model.Coverimage != null) { model.Imagepath = ""; var guid = Guid.NewGuid().ToString(); var path = Path.Combine( Directory.GetCurrentDirectory(), "wwwroot/Admin/writerimg", guid + ".jpg"); using (var stream = new FileStream(path, FileMode.Create)) { model.Coverimage.CopyTo(stream); } model.Imagepath = guid + ".jpg"; person.Imagepath = model.Imagepath; } _bookcontext.SaveChanges(); int PersonID = person.ID; List <PersonDuty> personduties = person.PersonDuties.ToList(); foreach (var item in personduties) { item.IsDeleted = true; } foreach (var item in dutyarray) { PersonDuty personduty = new PersonDuty(); personduty.PersonID = PersonID; personduty.DutyID = item; _bookcontext.PeopleDuty.Add(personduty); } } _bookcontext.SaveChanges(); return(Redirect("/Admin/Person/")); //Redirect("/SiteAccount/"); }
public IActionResult Add(PersonVM model, int[] dutyarray) { model.Imagepath = ""; if (model.Coverimage != null) { var guid = Guid.NewGuid().ToString(); var path = Path.Combine( Directory.GetCurrentDirectory(), "wwwroot/Admin/writerimg", guid + ".jpg"); using (var stream = new FileStream(path, FileMode.Create)) { model.Coverimage.CopyTo(stream); } model.Imagepath = guid + ".jpg"; } if (ModelState.IsValid) { Person person = new Person(); person.Name = model.Name; person.SurName = model.SurName; person.Biography = model.Biography; person.BirthDate = model.BirthDate; person.Imagepath = model.Imagepath; _bookcontext.People.Add(person); _bookcontext.SaveChanges(); int PersonID = person.ID; foreach (var item in dutyarray) { PersonDuty personduty = new PersonDuty(); personduty.PersonID = PersonID; personduty.DutyID = item; _bookcontext.PeopleDuty.Add(personduty); } _bookcontext.SaveChanges(); return(Redirect("/Admin/Person/")); } return(View()); }