문제

I am trying to validate input control in Asp.Net MVC. The below code checks if Project name is null and throws validation. I will have some default text in textbox on page load. I have a scenario to check if project name is not null and project name is not "Test Project". Can I do in similar scenario?

[Required(ErrorMessage = "Select Project Name.")]
[DisplayName("Project Type: ")]
public string SelectedProjectName { get; set; }
도움이 되었습니까?

해결책

you can write your own Custom validator here is an example.

    public class ProjectNameValidation : ValidationAttribute
{

    public ProjectNameValidation()
    {

    }

    //private const string errorMsg = "{0} must at least {1} or not more than {2}";

    public override bool IsValid(object value)
    {
        if (value != null && value !="Test Project")
        {
            return true;
        }

        return false;
    }
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top