// ############################################################ public Person AddPerson(Person newPerson) { // Set the identifier value to the max-plus-one of the existing identifier values newPerson.Id = (this.Persons.Count > 0) ? this.Persons.Max(max => max.Id) + 1 : 1; // Add the object this.Persons.Add(newPerson); // Save ctx.Session["persons"] = this.Persons; return newPerson; }
public ActionResult Create(Person newPerson) { if (ModelState.IsValid) { // Attempt to add the new Person object m.AddPerson(newPerson); return RedirectToAction("Index"); } return View(); }
// ############################################################ public Person EditPerson(Person editedPerson) { // Attempt to locate the existing object by using the passed-in identifier var existingPerson = this.Persons.SingleOrDefault(pid => pid.Id == editedPerson.Id); // If located... if (existingPerson != null) { // Get the object's index value in the collection int collectionIndex = this.Persons.IndexOf(existingPerson); // Replace the existing object with the new object this.Persons[collectionIndex] = editedPerson; // Save ctx.Session["persons"] = this.Persons; return editedPerson; } else { return null; } }
public ActionResult Edit(int id, Person updatedperson) { if (ModelState.IsValid) { // Attempt to update the Person object m.EditPerson(updatedperson); return RedirectToAction("Index"); } return View(); }