MVC2:使用 DataAnnotations 验证 DataType

发布于 2024-10-01 18:10:13 字数 951 浏览 4 评论 0原文

我正在使用实体框架 + SQL Server DB,并使用带有 DataAnnotations 的部分类来验证数据。对于像必需和范围这样的东西,这工作得很好,但我无法让数据类型验证器工作。

这是(自定义)注释的示例:

[DataTypeWholeNumberAttribute(ErrorMessage = "Zip must be a whole number")]
public object Zip{ get; set; }

...和控制器代码...

[HttpPost]   
public ActionResult Edit(NamedInsuredViewModel viewModel)   
{   
    try  
    { //breakpoint here (opening squiggly bracket) shows .Zip is already null   
        if (ModelState.IsValid)   
        ...save, etc...
    }
}

我知道发生了什么:数据库中 Zip 的 DataType 是 int,因此默认验证是捕获它并应用通用错误消息在我的验证器到达之前,“值 [x] 对于 [FieldName] 无效”(为了证明这一点,我还在字符串字段中添加了相同的验证器,并且它工作得很好)。我不知道的是,我该如何解决这个问题(不,我不能更改数据库以将字符串用于所有内容)?

这篇文章中提供了一些建议(http://forums.asp.net/ p/1608322/4162819.aspx#4162819),但到目前为止没有任何帮助。

提前致谢。

PS - 是否真的没有办法在不创建自定义属性的情况下验证原始数据类型?

I am using Entity Framework + SQL Server DB and am using partial classes with DataAnnotations to validate data. For things like Required and Range, this works fine, but I am unable to get the DataType validators to work.

Here is an example of the (custom) annotation:

[DataTypeWholeNumberAttribute(ErrorMessage = "Zip must be a whole number")]
public object Zip{ get; set; }

...and the Controller Code...

[HttpPost]   
public ActionResult Edit(NamedInsuredViewModel viewModel)   
{   
    try  
    { //breakpoint here (opening squiggly bracket) shows .Zip is already null   
        if (ModelState.IsValid)   
        ...save, etc...
    }
}

And I know what's happening: The DataType of Zip in the database is int, so the default validation is catching that and applying the generic error message "the value [x] is not valid for [FieldName]" before my validator can get to it (to prove this, I also added the same validator to a string field, and it works just fine). What I don't know is, how can I get around that (and no, I can't change the DB to use strings for everything)?

Some suggestions have been offered in this post (http://forums.asp.net/p/1608322/4162819.aspx#4162819), but so far nothing has helped.

Thanks in advance.

PS - is there really no way to validate a primitive DataType without creating a custom Attribute?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月下客 2024-10-08 18:10:13

我认为错误是将名为“viewModel”的东西传递给编辑操作。
ViewModel 旨在将数据传递到视图以呈现它。
当您提交表单时,数据必须映射到实体而不是视图模型。

[HttpPost]   
public ActionResult Edit(YourEntity entity)   
{   
    try  
    { //breakpoint here (opening squiggly bracket) shows .Zip is already null   
        if (ModelState.IsValid)   
        ...save, etc...
    }
}

I think the error is to pass something called "viewModel" to a Edit Action.
ViewModel is intended for pass data to a view to render it.
When you submit a form the data have to be mapped to a entity not to a viewModel.

[HttpPost]   
public ActionResult Edit(YourEntity entity)   
{   
    try  
    { //breakpoint here (opening squiggly bracket) shows .Zip is already null   
        if (ModelState.IsValid)   
        ...save, etc...
    }
}
不即不离 2024-10-08 18:10:13

将您的自定义验证器应用到该类。然后将类实例作为验证器的参数而不是字符串传递。然后,无论类型如何,您都可以对适当的属性执行验证。

Apply your custom validator to the class. Then pass in your class instance as the parameter for your validator instead of as a string. Then you can perform the validation on the appropriate property regardless of type.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文