public JsonResult Entry(ExerciseEntryModel model) { if (ModelState.IsValid) { using (var entities = new DietJournalEntities()) { ExerciseEntry entry = null; if (model.Id > 0) entry = entities.ExerciseEntries.FirstOrDefault(e => e.Id == model.Id); if (entry == null) { entry = new ExerciseEntry(); entities.ExerciseEntries.AddObject(entry); } entry.UserId = CurrentUserId.Value; entry.Description = model.Description; entry.EntryDate = model.ConsumedDate; entry.SavedDate = DateTime.Now; entry.Type = !String.IsNullOrEmpty(model.ExerciseType) ? int.Parse(model.ExerciseType) : 0; if (model.Favorite) { var favoriteModel = new ExerciseFavorite { Type = entry.Type, Description = entry.Description, UserId = CurrentUserId.Value }; entities.ExerciseFavorites.AddObject(favoriteModel); } entities.SaveChanges(); if (model.EntryValues != null && model.EntryValues.Count > 0) { foreach (var entryValue in model.EntryValues) { ExerciseEntryExerciseTypeValue value = null; if (!String.IsNullOrEmpty(entryValue.Id)) { var entryValueId = int.Parse(entryValue.Id); value = entities.ExerciseEntryExerciseTypeValues.FirstOrDefault(v => v.Id == entryValueId); } if (value == null) { value = new ExerciseEntryExerciseTypeValue { ExerciseTypeId = int.Parse(entryValue.EntryTypeValueId), ExerciseEntryId = entry.Id }; entities.ExerciseEntryExerciseTypeValues.AddObject(value); } value.Value = entryValue.Value; } entities.SaveChanges(); } } return Json(new { IsValid = true }); } return Json(new { IsValid = false, ErrorMessage = "" }); }