
Required: 'Surname is required unless the party is for an organization. Data annotation attributes are attached to the properties of the model class and enforce some validation criteria. Additionally, we can also use data annotation attributes from the namespace to do validations at the model level.
And, if it doesnt work, just write this in the section script in your View page that you are using the ViewModel like this: redundant rendered elements. HTML helpers are provided by ASP.NET MVC to do the input validations.On the client you can do something like: Once you put the required Data Annotation in your Field in the View Models, for example. Yield return new ValidationResult("Surname is required unless the party is for an organization" ) If (party_type = "P" & string.IsNullOrWhitespace(surname)) Public IEnumerable Validate( ValidationContext context )
/ AttributeUsage(AttributeTargets.Property AttributeTargets.Field AttributeTargets. /Note I changed your properties to strings to avoid casting.Īlso, if you have other validation errors from attributes, those will fire first and prevent the Validate method from being run so you only detect these errors if the attribute-based validation is ok. Uri.IsWellFormedUriString checks that the URL format is correct and does not require escaping. You will have to write some custom javascript to validate this rule on the client as the Validate method doesn't translate into the unobtrusive validation framework.

Data Annotations, which are included as part of ASP. You'll have to remove the RequredAttribute from the property as well. NET Core MVC validation pipeline to provide automatic validation.

These attributes are used to define metadata for ASP.NET. You can make your model inherit from IValidatableObject and then put your custom logic into the Validate method. ASP.NET MVC includes built-in attribute classes in the namespace.
