public ActionResult FamilyEdit(int id, PersonBaseEditFamily newItem)
        {
            if (ModelState.IsValid & id == newItem.Id)
            {
                // Attempt to do the update
                var editedItem = m.PersonEditRelations(newItem);

                if (editedItem == null)
                {
                    // There was a problem updating the object

                    ModelState.AddModelError("modelState", "There was an error. (The incoming data is invalid.)");

                    return RedirectToAction("details", new { id = newItem.Id });
                }
                else
                {
                    // Redirect
                    // Could do other things here
                    return RedirectToAction("details", new { id = editedItem.Id });
                }
            }
            // There was a problem updating the object
            // Re-create an edit form

            ModelState.AddModelError("modelState", "There was an error. (The incoming data is invalid.)");

            return RedirectToAction("details", new { id = newItem.Id });
        }
示例#2
0
        public PersonBaseWithRelations PersonEditRelations(PersonBaseEditFamily newItem)
        {
            // Validate the incoming data
            var fetchedObject = ds.Persons
                .Include("Father")
                .Include("Mother")
                .Include("Children")
                .SingleOrDefault(p => p.Id == newItem.Id);

            Person father = null;
            if (newItem.FatherId.HasValue)
            {
                father = ds.Persons.Find(newItem.FatherId.Value);
            }
            Person mother = null;
            if (newItem.MotherId.HasValue)
            {
                mother = ds.Persons.Find(newItem.MotherId.Value);
            }

            if (fetchedObject == null)
            {
                return null;
            }
            else
            {
                // Update the object with the incoming values
                ds.Entry(fetchedObject).CurrentValues.SetValues(newItem);

                // Update the associated objects

                if (father != null)
                {
                    fetchedObject.Father = father;
                    fetchedObject.FatherId = father.Id;
                }

                if (mother != null)
                {
                    fetchedObject.Mother = mother;
                    fetchedObject.MotherId = mother.Id;
                }

                // Handle the collection

                if (newItem.ChildrenIds.Count() > 0)
                {
                    // Clear the existing children collection
                    fetchedObject.Children.Clear();

                    // Go through the incoming collection
                    foreach (var childId in newItem.ChildrenIds)
                    {
                        var child = ds.Persons.Find(childId);
                        if (child != null)
                        {
                            fetchedObject.Children.Add(child);
                        }
                    }
                }

                ds.SaveChanges();

                return Mapper.Map<PersonBaseWithRelations>(fetchedObject);
            }
        }