好的,我有一个问题,ModelState
错误没有映射到正确的属性。
让我看看我能不能解释一下。
我有一个像这样的 ViewModel:
public class MyViewModel
{
public string Prop1 {get;set;}
public string Prop2 {get;set;}
....
}
在我看来,我有一个模型,它有一个我想要的这种类型的集合EditorTemplate
。所以它会生成MyViewModels[0].Prop1
,MyViewModels[1].Prop1
等。
问题是,当我ModelState
通过我调用的接口在我的服务层中设置错误时,该接口具有IValidationDictionary
模型状态的包装器,它不会将错误附加到视图中的正确行,而是附加到模型的末尾作为Prop1
.
编辑
这是 ModelStateWrapper(VB - 对不起!)
Public Class ModelStateWrapper
Implements IValidationDictionary
#Region "Private Members/Properties"
Private modelState As ModelStateDictionary
Public ReadOnly Property IsValid As Boolean Implements IValidationDictionary.IsValid
Get
Return modelState.IsValid
End Get
End Property
#End Region
#Region "Constructor(s)"
Public Sub New(modelState As ModelStateDictionary)
Me.modelState = modelState
End Sub
#End Region
#Region "Methods"
Public Sub AddError(key As String, message As String) Implements IValidationDictionary.AddError
modelState.AddModelError(key, message)
End Sub
#End Region
End Class