【发布时间】:2012-01-30 21:03:59
【问题描述】:
好的,我有一个问题,ModelState 错误没有映射到正确的属性。
让我看看能不能解释一下。
我有一个像这样的 ViewModel:
public class MyViewModel
{
public string Prop1 {get;set;}
public string Prop2 {get;set;}
....
}
在我看来,我有一个模型,它有一个这种类型的集合,我有一个 EditorTemplate 。所以它会生成MyViewModels[0].Prop1、MyViewModels[1].Prop1等。
问题是,当我通过一个名为 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
【问题讨论】:
-
没有看到任何环绕模型状态的代码,您如何期望任何人能够回答为什么它不能正常工作?
标签: asp.net-mvc asp.net-mvc-3 modelstate