public async Task <IActionResult> Save(IFormCollection form)
        {
            try
            {
                JObject parameters = form.ToJObject();
                string  recordID   = parameters.GetValue("RecordId").ToString();
                if (recordID != null)
                {
                    Record r = await db.Records.Include(m => m.TextData).ThenInclude(td => td.FormField)
                               .Include(u => u.NumericData).ThenInclude(td => td.FormField)
                               .Include(u => u.BooleanData).ThenInclude(td => td.FormField)
                               .Include(u => u.Form).ThenInclude(m => m.FormFormFields).ThenInclude(fff => fff.FormField)
                               .Include(u => u.Form).ThenInclude(m => m.FormFormFields).ThenInclude(fff => fff.FormField).ThenInclude(mo => mo.PublicMotherFormField)
                               .Where(m => m.RecordId == new Guid(recordID)).FirstOrDefaultAsync();

                    foreach (FormField ff in r.Form.FormFormFields.Select(fff => fff.FormField))
                    {
                        if (ff.FieldTypeId == FieldTypeEnum.Text)
                        {
                            if (parameters.GetValue("Field_" + ff.FormFieldId) != null)
                            {
                                string   newValue = parameters.GetValue("Field_" + ff.FormFieldId).ToString();
                                TextData td       = r.TextData.Where(m => m.FormField.FormFieldId == ff.FormFieldId).FirstOrDefault();
                                if (td == null)
                                {
                                    td = new TextData()
                                    {
                                        FormField = ff, Record = r, Id = Guid.NewGuid(), Value = newValue
                                    };
                                    r.TextData.Add(td);
                                    db.Entry(td).State = EntityState.Added;
                                    db.Entry(r).State  = EntityState.Modified;
                                }
                                else
                                {
                                    td.Value           = newValue;
                                    db.Entry(td).State = EntityState.Modified;
                                }
                            }
                        }
                        else if (ff.FieldTypeId == FieldTypeEnum.DateTime)
                        {
                            if (parameters.GetValue("Field_" + ff.FormFieldId) != null)
                            {
                                string   newValue = parameters.GetValue("Field_" + ff.FormFieldId).ToString();
                                DateTime myDT;
                                myDT = DateTime.ParseExact(newValue.Replace("{0:", " ").Replace("}", ""), formats, CultureInfo.InvariantCulture, DateTimeStyles.None);


                                string zulu = myDT
                                              .ToString("yyyy-MM-ddTHH\\:mm\\:sszzz");

                                TextData td = r.TextData.Where(m => m.FormField.FormFieldId == ff.FormFieldId).FirstOrDefault();
                                if (td == null)
                                {
                                    td = new TextData()
                                    {
                                        FormField = ff, Record = r, Id = Guid.NewGuid(), Value = zulu
                                    };
                                    r.TextData.Add(td);
                                    db.Entry(td).State = EntityState.Added;
                                    db.Entry(r).State  = EntityState.Modified;
                                }
                                else
                                {
                                    td.Value           = zulu;
                                    db.Entry(td).State = EntityState.Modified;
                                }
                            }
                        }
                        else if (ff.FieldTypeId == FieldTypeEnum.Choice)
                        {
                            if (parameters.GetValue("Field_" + ff.FormFieldId) != null)
                            {
                                string newValue = parameters.GetValue("Field_" + ff.FormFieldId).ToString();
                                await db.Entry(ff).Collection(m => m.FieldChoices).LoadAsync();

                                FieldChoice fc = ff.FieldChoices.Where(m => m.Text == newValue).FirstOrDefault();
                                if (ff.PublicMotherFormField != null)
                                {
                                    await db.Entry(ff.PublicMotherFormField).Collection(m => m.FieldChoices).LoadAsync();

                                    fc = ff.PublicMotherFormField.FieldChoices.Where(m => m.Text == newValue).FirstOrDefault();
                                }
                                TextData td = r.TextData.Where(m => m.FormField.FormFieldId == ff.FormFieldId).FirstOrDefault();
                                if (td == null)
                                {
                                    td = new TextData()
                                    {
                                        FormField = ff, Record = r, Id = Guid.NewGuid(), Value = newValue, FieldChoice = fc
                                    };
                                    r.TextData.Add(td);
                                    db.Entry(td).State = EntityState.Added;
                                    db.Entry(r).State  = EntityState.Modified;
                                }
                                else
                                {
                                    td.Value           = newValue;
                                    td.FieldChoice     = fc;
                                    db.Entry(td).State = EntityState.Modified;
                                }
                            }
                        }
                        else if (ff.FieldTypeId == FieldTypeEnum.Boolean)
                        {
                            if (parameters.GetValue("Field_" + ff.FormFieldId) != null)
                            {
                                bool        newValue = (bool)parameters.GetValue("Field_" + ff.FormFieldId).ToObject <bool>();
                                BooleanData bd       = r.BooleanData.Where(m => m.FormField.FormFieldId == ff.FormFieldId).FirstOrDefault();
                                if (bd == null)
                                {
                                    bd = new BooleanData()
                                    {
                                        FormField = ff, Record = r, Id = Guid.NewGuid(), Value = newValue
                                    };
                                    r.BooleanData.Add(bd);
                                    db.Entry(bd).State = EntityState.Added;
                                    db.Entry(r).State  = EntityState.Modified;
                                }
                                else
                                {
                                    bd.Value           = newValue;
                                    db.Entry(bd).State = EntityState.Modified;
                                }
                            }
                        }
                    }

                    User user = Helpers.UserHelper.GetCurrentUser(User, db);

                    ChangeLog cl = new ChangeLog()
                    {
                        Log = user.UserId + " changed the record", User = user
                    };
                    ChangeLogRecord clr = new ChangeLogRecord()
                    {
                        Record = r, ChangeLog = cl
                    };
                    if (r.RecordChangeLogs == null)
                    {
                        r.RecordChangeLogs = new List <ChangeLogRecord>();
                    }
                    r.RecordChangeLogs.Add(clr);

                    await db.SaveChangesAsync();
                }
                return(Content("OK"));
            }
            catch (Exception e)
            {
                return(Content(e.ToString()));;
            }
        }