-2

I have a asp.NET MVC 5 project with 4 assembly projects. Those are;

  1. Common
  2. Domain
  3. Service
  4. 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.

Sumudu De Zoysa
  • 261
  • 1
  • 6
  • 17
  • 1
    You don't have to use a view model, but you should. What is not working as expected? (also recommend you use `[EmailAddress]` rather than you `RegularExpression]` attribute) –  Dec 17 '15 at 04:39
  • Yes, that's correct. Thank u very much Stephen :) what a mistake i did. Thanks a lot. – Sumudu De Zoysa Dec 17 '15 at 04:51
  • 1
    Then perhaps you should consider voting and/or accepting the answer –  Dec 17 '15 at 07:31

1 Answers1

3

Your [RemoteAttribute] is not working because the method you are calling has an incorrect parameter. It needs to be

public JsonResult IsEmailExists(string email)

The name of the parameter must match the name of the property you apply the attribute to.

Having said that, you still should be using a view model. The int ID for example is not appropriate (the user has not been created yet). And attributes such as [Remote] and [Display] are not applicable to a data model. And assuming you need a password to register, your view model would need an additional property such as ConfirmPasswrd with a [Compare] attribute which would not be applicable in a data model.

Refer What is ViewModel in MVC? for more information on why you should be using a view model and its benefits.

Side note: I recommend you use the [EmailAddress] attribute in lieu of your [RegularExpression] attribute.

Community
  • 1
  • 1