public IActionResult Post([FromBody] PersonVM personvm) { using (var db = new DBContext()) { if (personvm.IsNew) { var personNew = new Person() { Name = personvm.Name, Family = personvm.Family, Subscribed = personvm.Subscribed, NationalCode = personvm.NationalCode, }; db.Attach(personNew); db.SaveChanges(); return(Ok(personvm)); } var person = db.Person.Find(personvm.Id); if (person != null) { person.Name = personvm.Name; person.Family = personvm.Family; person.Subscribed = personvm.Subscribed; person.NationalCode = personvm.NationalCode; foreach (var item in personvm.ThePersonPostList) { if (item.IsNew) { person.ThePersonPostList = new List <PersonPost>() { new MasterDetail.Web.Model.PersonPost() { Name = item.Name, Description = item.Description, } }; } else if (item.IsEdited) { var editedPersonPost = db.PersonPost.Find(item.Id); editedPersonPost.Name = item.Name; editedPersonPost.Description = item.Description; db.Attach(editedPersonPost); } else if (item.IsDeleted) { var deletedPersonPost = db.PersonPost.Find(item.Id); db.PersonPost.Remove(deletedPersonPost); } } } db.Attach(person); db.SaveChanges(); } return(Ok(personvm)); }
public IActionResult Delete([FromBody] PersonVM personvm) { using (var db = new DBContext()) { var person = db.Person.Include(i => i.ThePersonPostList) .SingleOrDefault(x => x.Id == personvm.Id); if (person != null) { db.Person.Remove(person); } db.SaveChanges(); } return(Ok(personvm.Id)); }