public bool Create(UnitBindingModel model) { try { using (var libraryDb = new LibraryManagementEntities()) { // check duplicated var currentUnit = libraryDb.Units.FirstOrDefault(s => s.Name == model.Name); if (currentUnit != null) { throw new ArgumentNullException(); } // Insert db var unitInfo = _entityMapper.Map <UnitBindingModel, Unit>(model); libraryDb.Units.Add(unitInfo); libraryDb.SaveChanges(); return(true); } } catch (Exception) { return(false); } }
public IActionResult Save(UnitBindingModel model) { if (!ModelState.IsValid) { DateTime?createdOn = (DateTime?)null; var createdBy = string.Empty; DateTime?lastUpdatedOn = (DateTime?)null; var lastUpdatedBy = string.Empty; var buildingName = string.Empty; if (model.UnitId > 0) { var u = _unitRepository.GetUnit(model.UnitId); createdOn = u.CreatedOn; createdBy = u.CreatedByName; lastUpdatedOn = u.LastUpdatedOn; lastUpdatedBy = u.LastUpdatedByName; buildingName = u.BuildingName; } return(View("Edit", new UnitViewModel { UnitId = model.UnitId, CreatedOn = createdOn, CreatedBy = createdBy, LastUpdatedOn = lastUpdatedOn, LastUpdatedBy = lastUpdatedBy, UnitName = model.UnitName, BuildingName = buildingName, SquareFootage = model.SquareFootage, NumberOfBedrooms = model.NumberOfBedrooms, NumberOfBathrooms = model.NumberOfBathrooms })); } var unit = new Unit(); if (model.UnitId > 0) { unit = _unitRepository.GetUnit(model.UnitId); } unit.UnitName = model.UnitName; unit.SquareFootage = model.SquareFootage; unit.NumberOfBedrooms = model.NumberOfBedrooms; unit.NumberOfBathrooms = model.NumberOfBathrooms; if (model.UnitId > 0) { _unitRepository.UpdateUnit(unit); } else { _unitRepository.AddUnit(unit); } return(RedirectToAction("Index")); }
private void btnSave_Click(object sender, EventArgs e) { bool add = txtId.Text == "" ? false : true; if (!add) { UnitBindingModel model = new UnitBindingModel() { Name = txtName.Text }; if (manager.Create(model)) { add = false; MessageBox.Show("Thêm mới thành công !", "Thông báo"); } else { MessageBox.Show("Thêm mới không thành công!. Liên hệ với quản trị viên.", "Thông báo"); } } else { UnitBindingModel model = new UnitBindingModel() { Id = int.Parse(txtId.Text), Name = txtName.Text }; if (manager.Update(model)) { MessageBox.Show("Sửa thành công !", "Thông báo"); } else { MessageBox.Show("Sửa không thành công!. Liên hệ với quản trị viên.", "Thông báo"); } } LoadData(Searching, PageIndex, PageSize); }
public bool Update(UnitBindingModel model) { try { using (var libraryDb = new LibraryManagementEntities()) { // Check first get item var unit = libraryDb.Units.FirstOrDefault(s => s.Id == model.Id); if (unit == null) { throw new ArgumentNullException("No exist"); } unit.Name = model.Name; libraryDb.SaveChanges(); return(true); } } catch (Exception) { return(false); } }