0

I have a ASP.NET MVC5-applikation that is built in VB. I have a form that gets data to a viewmodel, and I want to add server side validation to the field Age, but only if Alive is true. Here is my model:

Public Class MyViewModel
  Public Property Name As String
  Public Property Age As Integer
  Public Property Alive As Boolean
End Class

I found this and this question on SO. VB does not have Yield, so I tried the following:

Public Function Validate(validationContext As ValidationContext) As Enumerable(Of ValidationResult) Implements IValidatableObject.Validate
        Dim results As List(Of ValidationResult) = New List(Of ValidationResult)
        If Alive And Age > 120 Then
            results.Add(New ValidationResult("Too old."))

        Else
            results.Add(ValidationResult.Success)
        End If
        Return results
    End Function

Now, I get a null reference exception. How do I implement conditional validation in VB?

Community
  • 1
  • 1
hellogoodnight
  • 1,989
  • 7
  • 29
  • 57

0 Answers0