问题标签 [data-annotations]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
2 回答
1917 浏览

c# - 我可以将 MVC 2 DataAnnotation 属性添加到现有属性吗?

我正在使用生成的类作为模型,并且希望将 DataAnnotation 属性添加到它的某些属性中。由于它是生成的代码,我不想直接添加注释。还有其他方法可以将它们附加到属性上吗?

我考虑过让模型成为一个接口,并使用一个部分类来让生成的类订阅它。假设它甚至可以工作,是否有一个不太复杂的解决方案?

0 投票
1 回答
280 浏览

asp.net-mvc-2 - Getting buddy class information?

I'm trying to read buddy class metadata information for usage outside of the normal asp.net mvc 2 validation process. I thought it would be as simple as saying:

That works, but is returning me the entire types metadata information and not just what is in the buddy information. According to the MVC2 source ( It uses the buddy class support from DataAnnotations, ) it should support getting the buddy data information but I don't see the part in the code where it reflects into the Metadatatype() attribute.

Generally confused here. Did I miss an overload or something?

0 投票
1 回答
202 浏览

asp.net-mvc - 如何验证需要访问 xVal 中的存储库的属性?

我正在尝试使用 xVal 来验证新用户的注册。在尝试实现检查新用户尝试注册的用户名是否已被占用的逻辑时,我遇到了一个嗡嗡声。如果我的 User 实体不依赖于我的 UsersRepository,我似乎无法找到一种方法来实现这一点。这是我需要找到一种在我的用户实体中实现的方法:

关于如何在这种情况下继续使用 xVal 并使我的用户实体与我的用户存储库分离的任何想法?

0 投票
1 回答
7803 浏览

asp.net-mvc - 使用数据注释进行自定义验证

我正在使用数据注释来检查正在输入的数据,但是当涉及到更自定义的数据验证方式时,我被卡住了。

我需要对数据库运行查询以查看那里是否存在东西,然后如果出现“自定义 db-check 错误”,例如“公司名称已存在”,则向用户报告

如何与数据注释一起实现这样的事情?

我使用 3.5sp1 附带的 linq 和实体框架完成了所有查询等

/米

0 投票
2 回答
6901 浏览

c# - 使用数据注释的带有 Range 的 ErrorMessage

我有这个:

效果很好,但它不显示错误消息,它显示一些标准的“值 '554g' 对 ObjectLimit 字段无效。”

我该如何解决?

0 投票
1 回答
1019 浏览

asp.net-mvc - Asp.net MVC 2.0 DataAnnotations 验证不会发出正确的 JSON

我正在尝试使用 ASP.Net MVC 2.0 Beta 中的 DataAnnotations 验证器进行设置,但使用以下模型:

在我看来,以下代码:

一切几乎都是逐字逐句的。发出的是:

从来没有发出任何东西来告诉任何 JavaScript 验证库(jQuery 或 MS Ajax,没关系)来验证字段。出于显而易见的原因,验证确实发生在服务器端,但从不在客户端。

0 投票
3 回答
101829 浏览

asp.net-mvc - ASP.Net MVC 显示格式

在我的模型中,我的一个属性上有以下 DataAnnotations

所需的注释效果很好,我添加了另外 2 个来尝试删除时间。它使用绑定到视图中的输入

但是,每当加载视图时,我仍然会在输入框中显示时间。无论如何使用DataAnnotations删除它?

0 投票
1 回答
1362 浏览

javascript - ASP.NET MVC 2 RC - 使用数据注释的客户端验证因多个属性而失败

在模型上使用数据注释时,我遇到了为客户端验证发出的 JavaScript 的问题。这是一个模型示例,它可以很好地处理两个必填字段,然后是发出的 JavaScript:

只要我在 EmailAddress 字段上放置另一个属性,即正则表达式属性,JavaScript 就不再发出任何规则。请注意,在下面的 JavaScript 中,规则应该是一个空数组。这是更改和脚本。有任何想法吗?

任何想法是什么导致添加新属性时规则消失?

谢谢!

0 投票
3 回答
3565 浏览

c# - C# Model Validation - Validation Application Block or DataAnnotations?

I want to add validator attributes to my domain models (in an ASP.NET MVC app), and I'm trying to decide between 2 frameworks, the Validation Application Block and DataAnnotations. They appears to do similar tasks, so I want to pick the one which will be the most supported/used in the future. It seems DataAnnotations are newer (and built into the framework as of 3.5 SP1), so do people think the Validation Application Block will become obsolete?

[Marked as Community Wiki as this is subjective]

0 投票
1 回答
4819 浏览

asp.net-mvc - ASP.NET MVC:添加将 DisplayName 合并到自定义 ValidationAttribute 的自定义 ErrorMessage

我正在使用带有 DataAnnotations 的 ASP.NET MVC。我创建了以下自定义 ValidationAttribute ,它工作正常。

但是,出现的错误消息是标准的“字段 * 无效”。我想将其更改为:“[DisplayName] 必须介于 [minlength] 和 [maxlength] 之间”,但是我无法弄清楚如何从此类中获取 DisplayName 甚至字段的名称。

有人知道吗?