Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello,
I am not using AccountController or membership for creating users and role in my MVC Application.
I have my database that maintains all this.
I want to apply data validation for one of the field "EmpNo"- to check already exists.
my question is, can I use remote validator provided by MVC under class System.Web.Mvc if not how can I apply Custom validation to achieve this.

Thanks
Posted
Comments
Jameel VM 6-Sep-13 4:51am    
can u post the code what you have tried?

1 solution

My purpose is solved
I did as below,
Validation is working fine with the create view but its also working for the same field in Edit view,where I dont want this validation to apply.
How to solve this ?

C#
public class EmployeeViewModel
{

    [CustomValidation(typeof(EmployeeViewModel), "ValidateDuplicate")]
    [Required]
    [DisplayName("Employee Number")]
    public string EmpNo{ get; set; }

    public static ValidationResult ValidateDuplicate(string empno)
    {
      bool isValid;

      using(var db = new YourContextName) {
        if(db.EmployeeViewModel.Where(e => e.Username.Equals(empno)).Count() > 0)
       {
          isValid = false;
       } else {
          isValid = true;
       }
      }

      if (isValid)
      {
        return ValidationResult.Success;
      }
      else
      {
        return new ValidationResult("empno already exists");
      }

    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900