public ActionResult Edit(ChildCreationModel model)
 {
     if (ModelState.IsValid)
     {
         db.Entry(model.child).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Details", "ChildAbduction", new { id = model.child.tipstaffRecordID }));
     }
     return(View(model));
 }
        public ActionResult Create(int id, bool initial = false)
        {
            ChildCreationModel model = new ChildCreationModel(id);

            if (model.tipstaffRecord.caseStatus.sequence > 3)
            {
                TempData["UID"] = model.tipstaffRecord.UniqueRecordID;
                return(RedirectToAction("ClosedFile", "Error"));
            }
            model.initial = initial;
            return(View(model));
        }
        //
        // GET: /Child/Edit/5
        public ActionResult Edit(int id)
        {
            ChildCreationModel model = new ChildCreationModel();

            model.child = db.Children.Find(id);
            if (model.child.childAbduction.caseStatus.sequence > 3)
            {
                TempData["UID"] = model.child.childAbduction.UniqueRecordID;
                return(RedirectToAction("ClosedFile", "Error"));
            }
            return(View(model));
        }
        public ActionResult Create(ChildCreationModel model, string submitButton)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }
            try
            {
                ChildAbduction ca = db.ChildAbductions.Find(model.tipstaffRecordID);

                /* if
                 *  EldestChild is null or
                 *  new child is eldest
                 * Add the surname of this child to the CA record
                 */
                Child  curEldest  = ca.children.OrderBy(c => c.dateOfBirth).ThenBy(c => c.childID).FirstOrDefault();
                string newSurname = model.child.nameLast; //by default set to new childs name
                if (curEldest != null)
                {
                    if (model.child.dateOfBirth > curEldest.dateOfBirth)
                    {
                        newSurname = curEldest.nameLast;
                    }
                }
                ca.EldestChild = newSurname.ToUpper();
                //Add new child
                ca.children.Add(model.child);

                //Now save the changes
                db.SaveChanges();

                if (Request.IsAjaxRequest())
                {
                    string url = string.Format("window.location='{0}';", Url.Action("Details", "ChildAbduction", new { id = model.tipstaffRecordID }));
                    return(JavaScript(url));
                }
                else
                {
                    switch (submitButton)
                    {
                    case "Save and add new Child":
                        return(RedirectToAction("Create", "Child", new { id = model.tipstaffRecordID, initial = model.initial }));

                    case "Save,add new Respondent":
                        return(RedirectToAction("Create", "Respondent", new { id = model.tipstaffRecordID, initial = model.initial }));

                    case null:
                    default:
                        return(RedirectToAction("Details", "ChildAbduction", new { id = model.tipstaffRecordID }));
                    }
                }
            }
            catch (DbUpdateException)
            {
                if (Request.IsAjaxRequest())
                {
                    return(PartialView("_createChildRecordForRecord", model));
                }
                else
                {
                    return(View(model));
                }
            }
            catch (ValidationException ex)
            {
                ErrorModel errModel = new ErrorModel(2);
                errModel.ErrorMessage  = genericFunctions.GetLowestError(ex);
                TempData["ErrorModel"] = errModel;
                return(RedirectToAction("IndexByModel", "Error", errModel ?? null));
            }
            catch (DbEntityValidationException ex)
            {
                ErrorModel errModel = new ErrorModel(2);
                errModel.ErrorMessage  = genericFunctions.GetLowestError(ex);
                TempData["ErrorModel"] = errModel;
                return(RedirectToAction("IndexByModel", "Error", errModel ?? null));
            }
            catch (Exception ex)
            {
                ErrorModel errModel = new ErrorModel(2);
                errModel.ErrorMessage  = genericFunctions.GetLowestError(ex);
                TempData["ErrorModel"] = errModel;
                return(RedirectToAction("IndexByModel", "Error", errModel ?? null));
            }
        }
        public PartialViewResult _Create(int id)
        {
            ChildCreationModel model = new ChildCreationModel(id);

            return(PartialView("_createChildRecordForRecord", model));
        }