public ActionResult AddModule()
 {
     var moduleViewModel = new ModuleViewModel() { ModuleViewModels = new List<ModuleViewModel>() };
     var departmentInOrgId = _wcfService.InvokeService<IOrganizationService, List<DepartmentInOrganization>>((svc) => svc.GetDepartmentInOrganization(SessionContext.CurrentUser.OrganizationId.Value));
     moduleViewModel.DepartmentInOrg = departmentInOrgId;
     moduleViewModel.Departments = new List<Department>();
     foreach (var department in departmentInOrgId)
     {
         var departmentFromDb = _wcfService.InvokeService<IOrganizationService, Department>((svc) => svc.GetDepartmentById(department.DepartmentId.Value));
         moduleViewModel.Departments.Add(departmentFromDb);
         moduleViewModel.ModuleInDepartments = departmentFromDb.ModuleInDepartments.ToList();
         foreach (var mod in moduleViewModel.ModuleInDepartments)
         {
             var module = _wcfService.InvokeService<IOrganizationService, Module>((svc) => svc.GetModuleById(mod.ModuleId.Value));
             var mappedModule = Mapper.Map<Module, ModuleViewModel>(module);
             moduleViewModel.ModuleViewModels.Add(mappedModule);
         }
     }
     return View(moduleViewModel);
 }
 private void ValidateModuleViewModel(ModuleViewModel moduleViewModel)
 {
     if (moduleViewModel.DepartmentInOrgId == 0)
         ModelState.AddModelError("", "Please select a department.");
     if (moduleViewModel.ModuleName.IsEmpty() || moduleViewModel.ModuleName.IsNull())
         ModelState.AddModelError("ModuleName", "Module Name is mandatory.");
     if (moduleViewModel.ModuleDesc.IsEmpty() || moduleViewModel.ModuleDesc.IsNull())
         ModelState.AddModelError("ModuleDesc", "Module Description is mandatory.");
 }
 public ActionResult AddModule(ModuleViewModel moduleViewModel)
 {
     ValidateModuleViewModel(moduleViewModel);
     if (ModelState.IsValid)
     {
         moduleViewModel.ModuleInDepartments = new List<ModuleInDepartment>();
         moduleViewModel.ModuleViewModels = new List<ModuleViewModel>();
         var deptInOrg = new ModuleInDepartment()
         {
             DepartmentId = moduleViewModel.DepartmentInOrgId,
             Active = true
         };
         moduleViewModel.ModuleInDepartments.Add(deptInOrg);
         moduleViewModel.IsActive = true;
         var mappedModule = Mapper.Map<ModuleViewModel, Module>(moduleViewModel);
         _wcfService.InvokeService<IOrganizationService>((svc) => svc.AddModule(mappedModule));
         moduleViewModel.CreationSuccessful = true;
         moduleViewModel.Message = "The Module '{0}' is saved successfully.".ToFormat(moduleViewModel.ModuleName);
         var moduleInDept = _wcfService.InvokeService<IOrganizationService, List<ModuleInDepartment>>((svc) => svc.GetModuleInDepartment(moduleViewModel.DepartmentInOrgId));
         foreach (var mod in moduleInDept)
         {
             var module = _wcfService.InvokeService<IOrganizationService, Module>((svc) => svc.GetModuleById(mod.ModuleId.Value));
             var mappedModuleViewModel = Mapper.Map<Module, ModuleViewModel>(module);
             moduleViewModel.ModuleViewModels.Add(mappedModuleViewModel);
         }
     }
     return View(moduleViewModel);
 }