Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
281 views
in Technique[技术] by (71.8m points)

c# - Best Data annotation for a Decimal(18,2)

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 ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

Two Decimal Points

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

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

Max 18 digits

[Range(0, 9999999999999999.99)]

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

Result

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...