public JsonResult doesRoleNameExist(string roleName) { // string CurrentURL = System.Web.HttpContext.Current.Request.UrlReferrer.LocalPath.Substring(10); string CurrentURL = System.Web.HttpContext.Current.Request.UrlReferrer.Segments[2]; //ดูหน้าเพจว่าอยู่หน้า CREATE or EDIT if (CurrentURL.Equals("EditRole")) { //ถ้า อยู่ในหน้า EDIT จะเช็คจากไอดีอื่นว่ามีชื่ออื่นซ้ำไหมถ้าไม่ใช่ ID ตัวเอง string Id = System.Web.HttpContext.Current.Request.UrlReferrer.Query.Split('=')[1]; bool unique = _roleRepository.CheckUnique(roleName.Replace(" ", string.Empty), Id); if (unique) { return(Json("The role name has been taken", JsonRequestBehavior.AllowGet)); } return(Json(true, JsonRequestBehavior.AllowGet)); } else { //กรณีสร้างใหม่ไม่มี ID ให้เช็คจะตรวจสอบชื่อจากทุกอัน bool unique = _roleRepository.CheckUnique(roleName.Replace(" ", string.Empty), "CREATE"); if (unique) { return(Json("The role name has been taken", JsonRequestBehavior.AllowGet)); } return(Json(true, JsonRequestBehavior.AllowGet)); } }