I have a asp.NET MVC 5 project with 4 assembly projects. Those are;
- Common
- Domain
- Service
- Web.UI
I have a model class in domain layer.
public class Employee
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ID { get; set; }
[Required(ErrorMessage = "*Please insert employee's first name")]
[Display(Name = "First Name", Prompt = "First Name")]
public string firstName { get; set; }
[Required(ErrorMessage = "*Please insert employee's last name")]
[Display(Name = "Last Name", Prompt = "Last Name")]
public string lastName { get; set; }
[Required(ErrorMessage = "*Please insert employee's email address")]
[Display(Name = "E-Mail", Prompt = "E-Mail")]
[Remote("IsEmailExists", "Employee", ErrorMessage = "This email is already registered: please enter a different email.")]
[RegularExpression(@"^([0-9a-zA-Z]([\+\-_\.][0-9a-zA-Z]+)*)+@(([0-9a-zA-Z][-\w]*[0-9a-zA-Z]*\.)+[a-zA-Z0-9]{2,3})$",
ErrorMessage = "*This email is invalid. Please enter a valid email")]
public string email { get; set; }
}
The Controller class for Employee is in a Web.UI project/Layer. In model class for email attribute, I have used remote validation to check whether the email is existing when registering a new employee. Relavent method to check that is in the Employee Controller.
public JsonResult IsEmailExists(string UserEmail)
{
//check if any of the Email matches the UserEmail specified in the Parameter using the ANY extension method.
return Json(!db.Employees.Any(x => x.email == UserEmail), JsonRequestBehavior.AllowGet);
}
This is not working as I expected. I searched a solution for the problem and what I figured out is, I have to use View models, because I'm using assembly projects. But I have no idea about how to do this. If anybody can help, it would be a great help. Thanks.