public bool SaveRecord(RecordEditViewModel Recordedit) { if (Recordedit != null) { using (var context = new ApplicationDbContext()) { if (Guid.TryParse(Recordedit.RecordId, out Guid newGuid)) { var Record = new Record() { RecordId = newGuid, SubCategoryId = Recordedit.SelectedSubCategoryId, CategoryCode = Recordedit.SelectedCategoryCode, PeriodId = Recordedit.SelectedPeriodId, }; Record.Subcategory = context.Subcategories.Find(Recordedit.SelectedSubCategoryId); Record.Category = context.Categories.Find(Recordedit.SelectedCategoryCode); Record.Period = context.Periods.Find(Recordedit.SelectedPeriodId); context.Records.Add(Record); context.SaveChanges(); return(true); } } } // Return false if Recordedit == null or RecordId is not a guid return(false); }
public bool SaveRecord(RecordEditViewModel recordEdit) { if (recordEdit != null) { using (var context = new ApplicationDbContext()) { if (Guid.TryParse(recordEdit.RecordId, out Guid newGuid)) { var record = new Record() { RecordId = newGuid, RecordValue = recordEdit.RecordValue, ExpenseId = recordEdit.SelectedExpenseTypeId, SubcategoryId = recordEdit.SelectedSubcategoryId, CategoryId = recordEdit.SelectedCategoryCode, PeriodId = recordEdit.SelectedPeriodCode }; record.ExpenseType = context.ExpenseTypes.Find(recordEdit.SelectedExpenseTypeId); record.Subcategory = context.Subcategories.Find(recordEdit.SelectedSubcategoryId); record.Category = context.Categories.Find(recordEdit.SelectedCategoryCode); record.Period = context.Periods.Find(recordEdit.SelectedPeriodCode); context.Records.Add(record); context.SaveChanges(); return(true); } } } // Return false if customeredit == null or CustomerID is not a guid return(false); }
public RecordEditViewModel CreateRecord() { var cRepo = new SubcategoriesRepository(); var rRepo = new CategoriesRepository(); var pRepo = new PeriodsRepository(); var Record = new RecordEditViewModel() { RecordId = Guid.NewGuid().ToString(), Subcategories = cRepo.GetSubcategories(), Categories = rRepo.GetCategories(), periods = pRepo.GetPeriods() }; return(Record); }
public RecordEditViewModel CreateRecord() { var eRepo = new ExpenseTypeRepository(); var sRepo = new SubcategoryRepository(); var cRepo = new CategoriesRepository(); var pRepo = new PeriodsRepository(); var record = new RecordEditViewModel() { RecordId = Guid.NewGuid().ToString(), ExpenseTypes = eRepo.GetExpenseTypes(), Subcategories = sRepo.GetSubcategories(), Categories = cRepo.GetCategories(), Periods = pRepo.GetPeriods() }; return(record); }
public ActionResult Create([Bind(Include = "RecordId, RecordValue, SelectedExpenseTypeId, SelectedSubcategoryId, SelectedCategoryCode, SelectedPeriodCode")] RecordEditViewModel model) { try { if (ModelState.IsValid) { var repo = new RecordsRepository(); bool saved = repo.SaveRecord(model); if (saved) { return(RedirectToAction("Index")); } } // Handling model state errors is beyond the scope of the demo, so just throwing an ApplicationException when the ModelState is invalid // and rethrowing it in the catch block. throw new ApplicationException("Invalid model"); } catch (ApplicationException ex) { throw ex; } }