public JsonResult DeleteAsset(int id) { var Success = false; var Message = ""; try { db.Entry(new Asset { Id = id }).State = EntityState.Deleted; db.SaveChanges(); Success = true; Message = Asset.DELETE_SUCCESS; } catch (Exception e) { Message = Asset.DELETE_FAIL; } return(Json(new { Success, Message })); }
public ActionResult RoomManagement(RoomManagementViewModel vm) { var Success = false; var Message = ""; if (vm.Rooms != null) { for (var i = 0; i < vm.Rooms.Count; i++) { var roomValidation = Room.Validator.ValidateName(vm.Rooms[i].Name); if (roomValidation != null) { ModelState.AddModelError($"Rooms[{i}].Name", roomValidation); } } } if (vm.NewRooms != null) { for (var i = 0; i < vm.NewRooms.Count; i++) { var roomValidation = Room.Validator.ValidateName(vm.NewRooms[i].Name); if (roomValidation != null) { ModelState.AddModelError($"NewRooms[{i}].Name", roomValidation); } } } if (ModelState.IsValid) { try { if (vm.DeletedRooms != null) { foreach (var r in vm.DeletedRooms) { db.Entry(new Room { Id = r.Id }).State = EntityState.Deleted; } } if (vm.Rooms != null) { foreach (var r in vm.Rooms) { var room = db.Rooms.Find(r.Id); room.Name = r.Name; } } if (vm.NewRooms != null) { foreach (var r in vm.NewRooms) { db.Rooms.Add(new Room { Name = r.Name, }); } } db.SaveChanges(); Success = true; Message = Room.SAVE_SUCCESS; } catch (Exception e) { Message = Room.SAVE_FAIL; } return(Json(new { Success, Message })); } return(PartialView("_RoomManagement", vm)); }
public ActionResult UserPositionManagement(UserPositionManagementViewModel vm) { var Success = false; var Message = ""; if (vm.Positions != null) { for (var i = 0; i < vm.Positions.Count; i++) { var positionValidation = UserPosition.Validator.ValidateName(vm.Positions[i].Name); if (positionValidation != null) { ModelState.AddModelError($"Positions[{i}].Name", positionValidation); } } } if (vm.NewPositions != null) { for (var i = 0; i < vm.NewPositions.Count; i++) { var positionValidation = UserPosition.Validator.ValidateName(vm.NewPositions[i].Name); if (positionValidation != null) { ModelState.AddModelError($"NewPositions[{i}].Name", positionValidation); } } } if (ModelState.IsValid) { try { if (vm.DeletedPositions != null) { foreach (var r in vm.DeletedPositions) { db.Entry(new UserPosition { Id = r.Id }).State = EntityState.Deleted; } } if (vm.Positions != null) { foreach (var r in vm.Positions) { var position = db.UserPositions.Find(r.Id); position.Name = r.Name; } } if (vm.NewPositions != null) { foreach (var r in vm.NewPositions) { db.UserPositions.Add(new UserPosition { Name = r.Name, }); } } db.SaveChanges(); Success = true; Message = UserPosition.SAVE_SUCCESS; } catch (Exception e) { Message = UserPosition.SAVE_FAIL; } return(Json(new { Success, Message })); } return(PartialView("_UserPositionManagement", vm)); }
public ActionResult EditAssetModel(EditAssetModelViewModel vm) { var Success = false; var Message = ""; var categoryValidation = AssetModel.Validator.ValidateCategory(vm.SelectedCategoryId); if (categoryValidation != null) { ModelState.AddModelError("Category", categoryValidation); } var nameValidation = AssetModel.Validator.ValidateName(null, vm.Name, vm.SelectedCategoryId); if (nameValidation != null) { ModelState.AddModelError("Name", nameValidation); } var propertiesForValidation = new List <Tuple <string, bool> >(); var hasPropertyErrors = false; if (vm.Properties != null) { for (var i = 0; i < vm.Properties.Count; i++) { var propertyValidation = AssetModel.Validator.ValidateProperty(vm.Properties[i].Name); if (propertyValidation != null) { ModelState.AddModelError($"Properties[{i}].Name", propertyValidation); hasPropertyErrors = true; } } propertiesForValidation.AddRange((from p in vm.Properties select new Tuple <string, bool>(p.Name, p.IsNumeric)).ToList()); } if (vm.NewProperties != null) { for (var i = 0; i < vm.NewProperties.Count; i++) { var propertyValidation = AssetModel.Validator.ValidateProperty(vm.NewProperties[i].Name); if (propertyValidation != null) { ModelState.AddModelError($"NewProperties[{i}].Name", propertyValidation); hasPropertyErrors = true; } } propertiesForValidation.AddRange((from p in vm.NewProperties select new Tuple <string, bool>(p.Name, p.IsNumeric)).ToList()); } if (!hasPropertyErrors) { var propertiesValidation = AssetModel.Validator.ValidateProperties(propertiesForValidation); if (propertiesValidation != null) { ModelState.AddModelError("Properties", propertiesValidation); } } if (ModelState.IsValid) { try { var model = db.AssetModels.Find(vm.Id); model.Name = vm.Name; model.AssetModelCategoryId = vm.SelectedCategoryId; if (vm.DeletedProperties != null) { foreach (var p in vm.DeletedProperties) { db.Entry(new AssetModelProperty { Id = p.Id }).State = EntityState.Deleted; } } if (vm.Properties != null) { foreach (var p in vm.Properties) { var property = db.AssetModelProperties.Find(p.Id); property.Name = p.Name; property.IsNumeric = p.IsNumeric; } } if (vm.NewProperties != null) { foreach (var p in vm.NewProperties) { model.Properties.Add(new AssetModelProperty { AssetModel = model, Name = p.Name, IsNumeric = p.IsNumeric, }); } } db.SaveChanges(); Success = true; Message = AssetModel.SAVE_SUCCESS; } catch (Exception e) { Message = AssetModel.SAVE_FAIL; } return(Json(new { Success, Message })); } vm.Categories = db.CategoryDropdown(); return(PartialView("_EditAssetModel", vm)); }