简体   繁体   English

具有MVC2 RTM的DataAnnotationsModelBinder

[英]DataAnnotationsModelBinder with MVC2 RTM

Trying to validate models with DataAnnotations but DefaulModelBinder overrides my Required property error messages and never uses my error messages for invalid data entry. 尝试使用DataAnnotations验证模型,但DefaulModelBinder会覆盖我的Required属性错误消息,并且永远不会将我的错误消息用于无效数据输入。 Always show 'value' is invalid for 'property name'. 始终显示“值”对于“属性名称”无效。

In another question I saw that MVC 2 uses DataAnnotationsModelBinder but I couldn't find any class in MVC 2 binaries. 在另一个问题中,我看到MVC 2使用DataAnnotationsModelBinder,但是在MVC 2二进制文件中找不到任何类。 I downloaded the source for MVC futures and changed some source to compile it for .Net 4.0 but although I had success to compile, it has compatability problems and doesn't work as expected. 我下载了MVC期货的源代码,并更改了一些源代码以针对.Net 4.0进行编译,但是尽管我可以成功进行编译,但是它存在兼容性问题,并且无法按预期工作。

Any help is aprreciated. 任何帮助表示赞赏。

Do not try to use the DataAnnotationsModelBinder with MVC 2 RTM. 不要尝试将DataAnnotationsModelBinder与MVC 2 RTM一起使用。 MVC 2 RTM's DefaultModelBinder already contains all of the logic that was present in the DataAnnotationsModelBinder sample. MVC 2 RTM的DefaultModelBinder已经包含DataAnnotationsModelBinder示例中存在的所有逻辑。

This is happening because an exception is thrown in the DefaultModelBinder when it tries to cast the input value to the type of the receiving model property. 发生这种情况是因为,当DefaultModelBinder尝试将输入值转换为接收模型属性的类型时,将引发异常。

You can either use string as type for the properties on the receiving model or make a custom Model binder by implementing IModelBinder and handle the validation. 您可以将string用作接收模型上属性的类型,也可以通过实现IModelBinder并处理验证来创建自定义模型绑定器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM