简体   繁体   English

数据注释获取错误消息输出数据库

[英]Data-annotation getting errormessage out database

With asp.net mvc you can use the annotation [Required (errormessage="This is required")] 使用asp.net mvc,您可以使用注释[必需(errormessage =“这是必需的”)]

How can I create something like this: 我怎样才能创建这样的东西:

[Required (errormessage="ERRORXX")] [必填(errormessage =“ERRORXX”)]

So I can look up in a database what this ERRORXX is and display it on my form. 所以我可以在数据库中查找这个ERRORXX是什么,并将其显示在我的表单上。 Now my form displays ERRORXX. 现在我的表单显示ERRORXX。

How can I create something that solves my problem? 我怎样才能创造解决我问题的东西?

Thx! 谢谢!

Just an idea: why not pull the error messages from a resx file ? 只是一个想法:为什么不从resx文件中提取错误消息? I think this is the common way of doing this. 我认为这是做到这一点的常用方法。 It even allows you to localize your error messages easily. 它甚至允许您轻松地本地化您的错误消息。

I think that by using a resource file (resx file) it's even easier to change the error messages later on. 我认为通过使用资源文件(resx文件),以后更容易更改错误消息。 A resx file can be opened and edited in Word Pad for example. 例如,可以在Word Pad中打开和编辑resx文件。 You don't need to access a database with username/password, query it, etc. 您不需要使用用户名/密码访问数据库,查询它等。

Localizing ASP.NET MVC Validation 本地化ASP.NET MVC验证

Globalizing ASP.NET MVC Client Validation 全球化ASP.NET MVC客户端验证

Take a look here too: 看看这里:

Model Validation & Metadata in ASP.NET MVC 2 ASP.NET MVC 2中的模型验证和元数据

Customizing ASP.NET MVC 2 - Metadata and Validation 自定义ASP.NET MVC 2 - 元数据和验证

The default route to take is with Resources. 要采用的默认路由是Resources。 However, I understand your pain :) The way I've achieved it is a little unusual, but I'll give you a quick rundown. 但是,我理解你的痛苦:)我实现它的方式有点不寻常,但我会给你一个快速的破坏。 In our project, using resource files is not an option as its way too limited for our purposes, the details of which I won't bore you with now! 在我们的项目中,使用资源文件不是一个选项,因为它的方式对我们的目的来说太有限了,我现在不会厌烦你的细节! :) At it's most basic principle, we're setting the errorMessage property of the validation attribute to some sort of "key", and then just using that as a way to lookup the correct (languaged) response in our CMS database, when the validation fails (in our case using MVC, when we update the model and check the state - all at Controller level). :)根据它最基本的原则,我们将验证属性的errorMessage属性设置为某种“密钥”,然后将其用作在CMS数据库中查找正确(语言)响应的方法,验证失败(在我们的例子中使用MVC,当我们更新模型并检查状态时 - 全部在Controller级别)。

This is the same principle as using the resources (by specifying "ErrorMessageResourceName" and "ErrorMessageResourceType"), but you get to do what you want with it. 这与使用资源(通过指定“ErrorMessageResourceName”和“ErrorMessageResourceType”)的原理相同,但您可以使用它执行所需的操作。

To be clear, we originally extended the RequiredAttribute (as one example) with our own stuff, including putting in properly named arguments to allow us to retrieve a sensible CMS value from the database later on. 为了清楚起见,我们最初使用我们自己的东西扩展了RequiredAttribute(作为一个示例),包括输入正确命名的参数以允许我们稍后从数据库中检索合理的CMS值。 To be extra clear, we're using MVC and custom HtmlHelpers to render our own ValidationControls, which are what ultimately consume the custom values from our custom annotations, etc - None of this affects the dumbed-down principle here though, which is to just use "errorMessage" , or something like it, as a way to look up the actual message from where YOU want to, and WHEN you want to. 为了更清楚,我们使用MVC和自定义HtmlHelpers来渲染我们自己的ValidationControls,这最终消耗了我们自定义注释中的自定义值等等 - 这些都不会影响这里的愚蠢原则,这只是为了使用“errorMessage”或类似的东西,作为从您想要的位置查找实际消息的方式,以及您想要的时间。

I think you mean you want to read/use attribute declarations for a given property? 我想你的意思是你想要读取/使用给定属性的属性声明?

If so, you could either make your own RequiredAttribute class (to allow adding new or more appropriate properties as you wish). 如果是这样,您可以创建自己的RequiredAttribute类(以允许根据需要添加新的或更合适的属性)。 See: Attributes Tutorial 请参阅: 属性教程

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

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