是什么决定了集合属性项的 ModelStateDictionary 中的键
如果这是我的视图模型:
public class ViewModel{
public string SimpleProperty{get;set;}
public SubViewModel ComplexProperty{ get;set;}
public SubViewModel[] ComplexPropertyArray{ get; set; }
}
public class SubViewModel{
public string NestedSimpleProperty{get;set;}
}
那么分配给 ModelStateDictionary
的默认错误消息键是什么:
ViewModel.SimpleProperty< /s> (请参阅下面的更新)ViewModel.ComplexProperty(请参阅下面的更新)ViewModel.ComplexProperty.NestedSimpleProperty< em>(请参阅下面的更新)ViewModel.ComplexPropertyArray(请参阅下面的更新)- ViewModel.ComplexPropertyArray[0]
- ViewModel .ComplexPropertyArray[0].NestedSimpleProperty
更新 我在反射器中发现了这个:
protected internal static string CreateSubPropertyName(string prefix, string propertyName)
{
if (string.IsNullOrEmpty(prefix))
{
return propertyName;
}
if (string.IsNullOrEmpty(propertyName))
{
return prefix;
}
return (prefix + "." + propertyName);
}
所以,我认为这涵盖了除 #5 和 #6 之外的所有内容
If this is my view model:
public class ViewModel{
public string SimpleProperty{get;set;}
public SubViewModel ComplexProperty{ get;set;}
public SubViewModel[] ComplexPropertyArray{ get; set; }
}
public class SubViewModel{
public string NestedSimpleProperty{get;set;}
}
Then what would be the default error message keys assigned to a ModelStateDictionary
for:
ViewModel.SimpleProperty(see update below)ViewModel.ComplexProperty(see update below)ViewModel.ComplexProperty.NestedSimpleProperty(see update below)ViewModel.ComplexPropertyArray(see update below)- ViewModel.ComplexPropertyArray[0]
- ViewModel.ComplexPropertyArray[0].NestedSimpleProperty
Update I found this in reflector:
protected internal static string CreateSubPropertyName(string prefix, string propertyName)
{
if (string.IsNullOrEmpty(prefix))
{
return propertyName;
}
if (string.IsNullOrEmpty(propertyName))
{
return prefix;
}
return (prefix + "." + propertyName);
}
So, I think that covers everything except for #5 and #6
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您将
NestedSimpleProperty
设置为必需:然后您有一个表单,其中该属性有多个文本框,对应于
ComplexPropertyArray
集合中的每个项目,那么键将是用于错误消息的将是ComplexPropertyArray[i].NestedSimpleProperty
,其中i
表示数组中包含空值的元素的索引。If you make the
NestedSimpleProperty
required:and then you have a form in which you have multiple textboxes for this property corresponding to each item in the
ComplexPropertyArray
collection then the key that will be used for error messages will beComplexPropertyArray[i].NestedSimpleProperty
wherei
represents the index of the element in the array which contains an empty value.