asp.net mvc 3 数据类型验证
我正在尝试实现数据类型的验证。我已经使用了 DataAnnotations,但对于数据类型,它没有显示自定义消息,
例如当我尝试将字符串数据输入 int 类型字段时。在这种情况下我如何自定义消息?
I am trying to realize valition on data type. I have used DataAnnotations, but for data type it's not showing customized message
for example when I' am trying enter string data into int typed field. How I can customize messages in this case?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果我不得不猜测,您听起来像是希望在验证模型中的一个或多个字段时显示一条自定义消息。您可以子类化
DataAnnotations.ValidationAttribute
类并重写IsValid(object)
方法,最后设置自定义ErrorMessage
值(其中ErrorMessage
已经属于ValidationAttribute
类)最后,用属性装饰模型属性
如果您使用的是开箱即用的 MVC3,这应该是正确验证所需的全部内容模型(尽管您的模型可能会有所不同/具有更多属性等)因此,在您的
[HttpPost]
控制器操作中,MVC 将自动绑定MyClass
并且您将能够使用 ModelState.IsValid 确定发布的数据实际上是否有效。If I had to guess, you sound like you want a custom message to display when validating one or more fields in your model. You can subclass the
DataAnnotations.ValidationAttribute
class and override theIsValid(object)
method and finally setting a customErrorMessage
value (whereErrorMessage
already belongs to theValidationAttribute
class)Finally, decorate your model property with the attribute
If you're using out-of-the-box MVC3, this should be all you need to propertly validate a model (though your model will probably differ/have more properties, etc) So, in your
[HttpPost]
controller action, MVC will automagically bindMyClass
and you will be able to useModelState.IsValid
to determine whether or not the posted data is, in fact, valid.Pavel,
DataAnnotations DataType 属性不会影响验证。它用于决定如何呈现您的输入。在这种情况下,大卫的上述解决方案有效。
但是,如果您只想使用内置验证属性,则可能需要像这样使用 Range 属性:(
当然,您确实应该使用 ErrorMessageResourceName/Type 参数并将硬编码的错误消息字符串提取到resx 文件。)
确保让 MVC 知道在哪里呈现错误消息:
或者您可以只使用 EditorForModel,它会正确设置它。
Pavel,
The DataAnnotations DataType attribute does not affect validation. It's used to decide how your input is rendered. In such a case, David's solution above works.
However, if you want to use only the built-in validation attributes, you probably need to use the Range attribute like this:
(Of course, you should really be using the ErrorMessageResourceName/Type parameters and extract out hard-coded error message strings into resx files.)
Make sure to let MVC know where to render your error message:
Or you can just use EditorForModel and it will set it up correctly.
我认为这个问题还没有得到解答,因为我也有同样的问题。
如果您的模型具有 int 类型的属性,并且用户输入字符串“asd”,则 MVC3 框架绑定/验证将介入并导致您的视图显示
“值 'asd' 对于以下内容无效<此处为模型属性名称或 DisplayName>"
。对我来说,发帖者问 MVC3 框架输出的这条消息可以定制吗?
我也想知道。虽然该消息还不错,但如果您将字段标记为可以轻松指示需要数字的内容,您可能仍希望包含其他原因,因此它会显示如下内容:
“值 'asd' 对于 < 无效;fieldname>; 必须是正整数。”
这样用户就不会输入一个又一个的值,并且每次都会收到不同的错误消息。
I don't think this has been answered because I have the same issue.
If you have a Model with a property of type int and the user types in a string of "asd" then the MVC3 framework binding/validation steps in and results in your view displaying
"The value 'asd' is not valid for <model property name or DisplayName here>"
.To me the poster is asking can this message that the MVC3 framework is outputting be customized?
I'd like to know too. Whilst the message is not too bad if you label your field something that easily indicates an number is expected you might still want to include additional reasons so it says something like:
"The value 'asd' is not valid for <fieldname>; must be a positive whole number."
So that the user is not entering value after value and getting different error messages each time.