public ActionResult CompanyCreate([DataSourceRequest] DataSourceRequest request, CompanyViewModel companyViewModel) { try { if (companyViewModel != null && ModelState.IsValid) { using (ASJDE context = new ASJDE()) { Company company = new Company { ID = companyViewModel.ID, EntityID = companyViewModel.EntityID }; context.Companies.Add(company); context.SaveChanges(); companyViewModel.ID = company.ID; } } return Json(new[] { companyViewModel }.ToDataSourceResult(request, ModelState)); } catch (Exception e) { logger.Error("Exception", e); return null; } }
private IEnumerable<CompanyViewModel> GetCompanyViewModels() { //List<StrategicBusinessUnitViewModel> sbuViewModels = new List<StrategicBusinessUnitViewModel>(); List<CompanyViewModel> companyViewModels = new List<CompanyViewModel>(); try { using (ASJDE context = new ASJDE()) { IQueryable<Company> companies = (from c in context.Companies select c); foreach (Company company in companies) { CompanyViewModel companyViewModel = new CompanyViewModel(); companyViewModel.Name = company.Entity.Name; companyViewModel.StrategicBusinessUnitViews = new List<StrategicBusinessUnitViewModel>(); Company co = company; IQueryable<StrategicBusinessUnit> sbus = (from s in context.StrategicBusinessUnits where s.Entity.EntityStatus.ID == 1 && s.CompanyID == co.ID select s); foreach (var strategicBusinessUnit in sbus) { StrategicBusinessUnitViewModel sbuViewModel = new StrategicBusinessUnitViewModel(); sbuViewModel.Name = strategicBusinessUnit.Entity.Name; sbuViewModel.OBUViewModels = new List<OperationalBusinessUnitViewModel>(); StrategicBusinessUnit sbUnit = strategicBusinessUnit; IQueryable<OperationalBusinessUnit> obs = (from o in context.OperationalBusinessUnits where o.Entity.EntityStatus.ID == 1 && o.StrategicBusinessUnitID == sbUnit.ID select o); foreach (var operationalBusinessUnit in obs) { OperationalBusinessUnitViewModel obuViewModel = new OperationalBusinessUnitViewModel(); obuViewModel.Name = operationalBusinessUnit.Entity.Name; obuViewModel.BUViewModels = new List<BusinessUnitViewModel>(); OperationalBusinessUnit bUnit = operationalBusinessUnit; IQueryable<BusinessUnit> bus = (from b in context.BusinessUnits where b.Entity.EntityStatus.ID == 1 && b.OperationalBusinessUnitID == bUnit.ID select b); foreach (var bu in bus) { BusinessUnitViewModel buViewModel = new BusinessUnitViewModel(); buViewModel.Name = bu.Entity.Name; buViewModel.BranchViewModels = new List<BranchViewModel>(); BusinessUnit bu1 = bu; IQueryable<Branch> branchs = (from br in context.Branches where br.Entity.EntityStatus.ID == 1 && br.BusinessUnitID == bu1.ID select br); foreach (var branch in branchs) { BranchViewModel branchViewModel = new BranchViewModel(); branchViewModel.Name = branch.Entity.Name; branchViewModel.DepotViewModels = new List<DepotViewModel>(); Branch branch1 = branch; var depots = (from d in context.Depots where d.Entity.EntityStatus.ID == 1 && d.BranchID == branch1.ID select d); foreach (var depot in depots) { DepotViewModel depotViewModel = new DepotViewModel(); depotViewModel.Name = depot.Entity.Name; branchViewModel.DepotViewModels.Add(depotViewModel); } buViewModel.BranchViewModels.Add(branchViewModel); } obuViewModel.BUViewModels.Add(buViewModel); } sbuViewModel.OBUViewModels.Add(obuViewModel); } companyViewModel.StrategicBusinessUnitViews.Add(sbuViewModel); } companyViewModels.Add(companyViewModel); } } } catch (Exception e) { logger.Error("Exception", e); return null; } return companyViewModels; }