public ActionResult CreateEmp(Employee employee) { if (ModelState.IsValid) { string UPDATEDATA = "INSERT"; foreach (EMPLOYEE_PHONE phone in employee.Phones.ToList()) { if (phone.DeletePhone == true) { // Delete Phone Numbers which is marked to remove employee.Phones.Remove(phone); } if (phone.Phone == null) { // Delete Phone Numbers When Phone = null employee.Phones.Remove(phone); } } //เช็ค เบอร์ซ้ำ List <EMPLOYEE_PHONE> pList = new List <EMPLOYEE_PHONE>(employee.Phones); for (int i = 0; i < pList.Count; i++) { if (pList[i].Phone.Length < 6 || pList[i].Phone.Length > 15) { ModelState.AddModelError("", "Phone number should be less than {9} characters."); return(View(employee)); } for (int j = 0; j < pList.Count; j++) { if (pList[i].Phone.Equals(pList[j].Phone) && i != j) { ModelState.AddModelError("", "Please complete the following information,Phone Number Fail."); return(View(employee)); } } } if (_empRepository.InsertEmpAndPhone(Mapper.ToDto(employee), employee.Phones.ToList(), UPDATEDATA) == false) { ModelState.AddModelError("", "Please complete the following information,Phone Number Fail."); return(View(employee)); } return(RedirectToAction("ListEmp2")); } ModelState.AddModelError("", "Please complete the following information."); return(View(employee)); }