public ActionResult Create([Bind(Include = "Name,LocationTypeId,CrimeTypeIds")]CrimeLocationTypeFormViewModel crimeLocationTypeForm) { if (ModelState.IsValid) { CrimeLocationTypeEntity crimeLocationTypeEntity = new CrimeLocationTypeEntity(); crimeLocationTypeEntity.Name = crimeLocationTypeForm.Name; crimeLocationTypeEntity.LocationType = _locationTypeService.GetLocationTypeById(crimeLocationTypeForm.LocationTypeId); crimeLocationTypeEntity.LocationTypeId = crimeLocationTypeForm.LocationTypeId; crimeLocationTypeEntity.CrimeTypes = new List<CrimeTypeEntity>(); foreach (var crimeTypeId in crimeLocationTypeForm.CrimeTypeIds) { crimeLocationTypeEntity.CrimeTypes.Add(_crimeTypeService.GetCrimeTypeById(crimeTypeId)); } _crimeLocationTypeService.CreateCrimeLocationType(crimeLocationTypeEntity); return RedirectToAction("Index"); } List<LocationTypeEntity> dbLocationTypeModels = _locationTypeService.GetAllLocationTypes().ToList(); List<CrimeTypeEntity> dbCrimeTypeModels = _crimeTypeService.GetAllCrimeTypes().ToList(); ViewData["LocationTypes"] = new SelectList(dbLocationTypeModels, "LocationTypeId", "Name"); ViewData["CrimeTypes"] = new MultiSelectList(dbCrimeTypeModels, "CrimeTypeId", "Name"); return View(crimeLocationTypeForm); }
public int CreateCrimeLocationType(CrimeLocationTypeEntity crimeLocationTypeEntity) { using (var scope = new TransactionScope()) { CrimeLocationType crimeLocationType = new CrimeLocationType(); // crimeLocationType.Name = crimeLocationTypeEntity.Name; crimeLocationType.LocationType = _unitOfWork.LocationTypeRepository.GetByID(crimeLocationTypeEntity.LocationTypeId); crimeLocationType.LocationTypeId = crimeLocationType.LocationType.LocationTypeId; List<CrimeType> crimeTypes = new List<CrimeType>(); foreach (var entity in crimeLocationTypeEntity.CrimeTypes) { crimeTypes.Add(_unitOfWork.CrimeTypeRepository.GetByID(entity.CrimeTypeId)); } crimeLocationType.CrimeTypes = crimeTypes; // _unitOfWork.CrimeLocationTypeRepository.Insert(crimeLocationType); _unitOfWork.Save(); scope.Complete(); return crimeLocationType.CrimeLocationTypeId; } }
public bool UpdateCrimeLocationType(int crimeLocationTypeId, CrimeLocationTypeEntity crimeLocationTypeEntity) { var success = false; if(crimeLocationTypeEntity != null) { using(var scope = new TransactionScope()) { CrimeLocationType crimeLocationType = _unitOfWork.CrimeLocationTypeRepository.GetByID(crimeLocationTypeId); if(crimeLocationType != null) { // crimeLocationType.CrimeLocationTypeId = crimeLocationTypeEntity.CrimeLocationTypeId; crimeLocationType.Name = crimeLocationTypeEntity.Name; crimeLocationType.LocationType = _unitOfWork.LocationTypeRepository.GetByID(crimeLocationTypeEntity.LocationType.LocationTypeId); crimeLocationType.LocationTypeId = crimeLocationTypeEntity.LocationType.LocationTypeId; crimeLocationType.CrimeTypes = new List<CrimeType>(); foreach (var entity in crimeLocationTypeEntity.CrimeTypes) { crimeLocationType.CrimeTypes.Add(_unitOfWork.CrimeTypeRepository.GetByID(entity.CrimeTypeId)); } // _unitOfWork.CrimeLocationTypeRepository.Update(crimeLocationType); _unitOfWork.Save(); scope.Complete(); success = true; } } } return success; }