Is it possible, given a model state key of answersToQuestions[0].answer or [0].SelectedWorkFunctionId, to get the value of a display annotation using reflection?
It is easy to do if it's just a single field in an object, but when the model is composed of other models or is a list, using the model state key with reflection we cannot get the data annotations for the property.
For i = 0 To ModelState.Keys.Count - 1
Dim field = model.GetType().GetProperty(ModelState.Keys(i))
If Not IsNothing(field) Then
Dim obj As New ViewErrorObjectModel
obj.HTML_Id = ModelState.Keys(i).Replace("[", "_").Replace("]", "_").Replace(".", "_")
obj.LabelValue = field.GetCustomAttributes(GetType(DisplayAttribute), False).Cast(Of DisplayAttribute)().SingleOrDefault().Name
obj.ErrorMessages.Add((From m In ModelState.Values(i).Errors Select m.ErrorMessage).FirstOrDefault)
errorObjectModelList.Add(obj)
End If
Next
Aucun commentaire:
Enregistrer un commentaire