public void FillEmployeeGrid() { if (EmployeeList.Count() > 0) { EmployeeList.Clear(); } using (var db = new DataSmartDBContext()) { var emp = db.Employee.ToList(); if (emp != null) { foreach (var d in emp) { EmployeeList.Add(d); } } } }
public async Task <ActionResult> Edit([Bind(Prefix = "UserLeavePolicy", Include = "Id,UserId,Description,WeeklyOffDays,AgreementType,FiscalYearStart,FiscalYearEnd,FiscalYearPeriod,Remarks")] UserLeavePolicy userLeavePolicy, [Bind(Prefix = "UserLeavePolicyDetail", Include = "LeaveTypeId,Allowed")] List <UserLeavePolicyDetail> userLeavePolicyDetail, [Bind(Prefix = "AnnualOffDay", Include = "Id,OffDay,Description")] List <AnnualOffDay> AnnualOffDays, List <string> DepartmentList, List <string> EmployeeList, string PolicyFor) { //UserLeavePolicy userLeavePolicyOld = await db.UserLeavePolicies.FindAsync(userLeavePolicy.Id); //db.UserLeavePolicyDetails.RemoveRange(userLeavePolicyOld.UserLeavePolicyDetails); //await db.SaveChangesAsync(); userLeavePolicy.WeeklyOffDays = userLeavePolicy.WeeklyOffDays; //"6,0"; userLeavePolicy.CountryId = 5; //from which user is Login. but admin who can view all coutries there we have to user a list of country so that he choose a country //userLeavePolicy.AnnualOffDays = string.Join(",", AnnualOffDays); userLeavePolicy.DepartmentPolicy = (PolicyFor == "1") ? true : false; foreach (UserLeavePolicyDetail item in userLeavePolicyDetail.ToList <UserLeavePolicyDetail>()) { if (item.Allowed == null) { //userLeavePolicyDetail.Remove(item); item.Allowed = 0; } item.UserLeavePolicyId = userLeavePolicy.Id; } if (ModelState.IsValid) { if (PolicyFor == "1")//department { CountryName dep; IQueryable <AspNetUser> usersFilterd = db.AspNetUsers.Where(x => x.UserLeavePolicyId == userLeavePolicy.Id); List <string> oldDeps = new List <string>(); oldDeps = usersFilterd.Select(p => p.CntryName).Distinct <string>().ToList <string>(); foreach (string itm in oldDeps)//get all employees of the deps, selected from UI { dep = db.CountryNames.FirstOrDefault(x => x.Name == itm); foreach (AspNetUser aspNetUser in dep.AspNetUsers) { //EmployeeList.Add(aspNetUser.Id); aspNetUser.UserLeavePolicyId = null; db.AspNetUsers.Attach(aspNetUser); db.Entry(aspNetUser).Property(x => x.UserLeavePolicyId).IsModified = true; } } EmployeeList = new List <string>(); foreach (string itm in DepartmentList)//get all employees of the deps, selected from UI { dep = db.CountryNames.FirstOrDefault(x => x.Name == itm); foreach (AspNetUser aspNetUser in dep.AspNetUsers) { //EmployeeList.Add(aspNetUser.Id); aspNetUser.UserLeavePolicyId = userLeavePolicy.Id; db.AspNetUsers.Attach(aspNetUser); db.Entry(aspNetUser).Property(x => x.UserLeavePolicyId).IsModified = true; } } await db.SaveChangesAsync(); } else { AspNetUser user; IQueryable <AspNetUser> oldUsers = db.AspNetUsers.Where(x => x.UserLeavePolicyId == userLeavePolicy.Id); //List<int> oldDeps = new List<int>(); //oldDeps = usersFilterd.Select(p => p.CountryId).Distinct<int>().ToList<int>(); //foreach (int itm in oldDeps)//get all employees of the deps, selected from UI //{ // dep = db.Countries.FirstOrDefault(x => x.Id == itm); if (oldUsers != null && oldUsers.Count() > 0) { foreach (AspNetUser aspNetUser in oldUsers) { //EmployeeList.Add(aspNetUser.Id); aspNetUser.UserLeavePolicyId = null; db.AspNetUsers.Attach(aspNetUser); db.Entry(aspNetUser).Property(x => x.UserLeavePolicyId).IsModified = true; } } //} if (EmployeeList != null && EmployeeList.Count() > 0) { foreach (string userId in EmployeeList) { user = db.AspNetUsers.FirstOrDefault(x => x.Id == userId); user.UserLeavePolicyId = userLeavePolicy.Id; db.AspNetUsers.Attach(user); db.Entry(user).Property(x => x.UserLeavePolicyId).IsModified = true; } } await db.SaveChangesAsync(); } ///////////////To delete old leve policy detils. if we dont.... new lines will be added with old ones/////////// IQueryable <UserLeavePolicyDetail> oldLPD = db.UserLeavePolicyDetails.AsQueryable().Where(x => x.UserLeavePolicyId == userLeavePolicy.Id); db.UserLeavePolicyDetails.RemoveRange(oldLPD); /////////////// db.Entry(userLeavePolicy).State = EntityState.Modified; db.UserLeavePolicyDetails.AddRange(userLeavePolicyDetail); await db.SaveChangesAsync(); return(RedirectToAction("Index")); } //ViewBag.UserId = new SelectList(db.AspNetUsers, "Id", "Hometown", userLeavePolicy.UserId); return(View(userLeavePolicy)); }