十进制的最佳数据注释(18,2) [英] Best Data annotation for a Decimal(18,2)

查看:134
本文介绍了十进制的最佳数据注释(18,2)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在SQL Server 2008中的列类型为Decimal(18,2).但是,在实体框架上,什么可以在asp.net MVC Web应用程序中应用于此属性的最佳数据注释验证是什么?

I have a column inside my sql server 2008 wih type of Decimal(18,2). But on entity framework what is the best data annotation validation I can apply to this property, inside my asp.net MVC web application ?

推荐答案

小数点没有显式的数据注释,因此您需要使用两个单独的注释来添加约束.

There is no explicit data annotation for a decimal so you need to use two separate ones to add constraints.

[RegularExpression(@"^\d+\.\d{0,2}$")]

此正则表达式将确保该属性最多具有两个小数位.

This regular expression will make sure that the property has at most two decimal places.

[Range(0, 9999999999999999.99)]

假设您不接受任何负数.否则,将0替换为-9999999999999999.99.

Assuming you aren't accepting any negative numbers. Otherwise, replace 0 with -9999999999999999.99.

[RegularExpression(@"^\d+\.\d{0,2}$")]
[Range(0, 9999999999999999.99)]
public decimal Property { get; set; }

这篇关于十进制的最佳数据注释(18,2)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆