public async Task <IActionResult> Edit([Bind("AccDptId,EngId,EngIdOrigin,DptName")] EngsInDptsModel engsInDptsModel) { var usr = _context.AppUsers.Find(engsInDptsModel.EngId); if (usr != null) { engsInDptsModel.UserName = usr.UserName; ModelState.Remove("UserName"); } if (ModelState.IsValid) { try { var isExist = _context.EngsInDpts.Find(engsInDptsModel.AccDptId, engsInDptsModel.EngId); if (isExist != null) // Data is already in DB. { ModelState.AddModelError("", "該部門已設定相同工程師!"); } else { // Get origin data and delete. var originModel = _context.EngsInDpts.Find(engsInDptsModel.AccDptId, engsInDptsModel.EngIdOrigin); _context.EngsInDpts.Remove(originModel); // Add new data. engsInDptsModel.Rtp = _context.AppUsers.Where(a => a.UserName == User.Identity.Name).First().Id; engsInDptsModel.Rtt = DateTime.Now; _context.EngsInDpts.Add(engsInDptsModel); await _context.SaveChangesAsync(); } } catch (DbUpdateConcurrencyException) { if (!EngsInDptsModelExists(engsInDptsModel.AccDptId)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } // Get all Med Engineers to dropdownlist. List <SelectListItem> list = new List <SelectListItem>(); List <string> s = new List <string>(); SelectListItem li; AppUserModel u; s = roleManager.GetUsersInRole("MedEngineer").ToList(); list = new List <SelectListItem>(); if (s.Count() > 0) { foreach (string l in s) { u = _context.AppUsers.Where(ur => ur.UserName == l).FirstOrDefault(); if (u != null) { li = new SelectListItem(); li.Text = u.FullName + "(" + u.UserName + ")"; li.Value = u.Id.ToString(); list.Add(li); } } } ViewData["EngId"] = new SelectList(list, "Value", "Text", engsInDptsModel.EngId); // engsInDptsModel.DptName = _context.Departments.Find(engsInDptsModel.AccDptId).Name_C; return(View(engsInDptsModel)); }
public async Task <IActionResult> Create([Bind("AccDptId,EngId")] EngsInDptsModel engsInDptsModel) { ModelState.Remove("UserName"); if (ModelState.IsValid) { var isExist = _context.EngsInDpts.Find(engsInDptsModel.AccDptId, engsInDptsModel.EngId); if (isExist != null) // Data is already in DB. { ModelState.AddModelError("", "該部門已設定相同工程師!"); } else { engsInDptsModel.UserName = _context.AppUsers.Find(engsInDptsModel.EngId).UserName; engsInDptsModel.Rtp = _context.AppUsers.Where(a => a.UserName == User.Identity.Name).First().Id; engsInDptsModel.Rtt = DateTime.Now; _context.Add(engsInDptsModel); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } } // // Get all Med Engineers to dropdownlist. List <SelectListItem> list = new List <SelectListItem>(); List <string> s = new List <string>(); SelectListItem li; AppUserModel u; s = roleManager.GetUsersInRole("MedEngineer").ToList(); list = new List <SelectListItem>(); if (s.Count() > 0) { foreach (string l in s) { u = _context.AppUsers.Where(ur => ur.UserName == l).FirstOrDefault(); if (u != null) { li = new SelectListItem(); li.Text = u.FullName + "(" + u.UserName + ")"; li.Value = u.Id.ToString(); list.Add(li); } } } ViewData["EngId"] = new SelectList(list, "Value", "Text"); // Get all departments. //var dptList = new[] { "K", "P", "C" }; //本院部門 //var departments = _context.Departments.Where(d => dptList.Contains(d.Loc)).ToList(); var departments = _context.Departments.ToList(); List <SelectListItem> listItem = new List <SelectListItem>(); foreach (var item in departments) { listItem.Add(new SelectListItem { Text = item.Name_C + "(" + item.DptId + ")", Value = item.DptId }); } ViewData["AccDptId"] = new SelectList(listItem, "Value", "Text"); return(View(engsInDptsModel)); }