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 ?
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");
}
}
}