Solved

Custom attributes using c#

Posted on 2014-12-02
4
107 Views
Last Modified: 2014-12-07
I am using data annotations to validate Model properties using MVC5 and need to validate a decimal property which should not be greater than 199.99.I am looking for sample code for custom attribute "NotGreaterThan" which should not be greater than 199.99.ie. if user enters quantity greater than 199.99, it should show message"The quantity can't be greater that 199.99".

       
[Required(ErrorMessage = "The Quantity is required")]
        [NotGreaterThan(199.99)]  //[b]custom attribute[/b]
        public decimal Quantity { get; set; }

Open in new window


Thanks in advance
0
Comment
Question by:ksd123
  • 2
4 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 40476987
Untested, but you might try:

using System;
using System.ComponentModel.DataAnnotations;

namespace WebApplication6
{
    [AttributeUsage(AttributeTargets.Property)]
    public class NotGreaterThanAttribute : ValidationAttribute
    {
        private double maxValue;

        public NotGreaterThanAttribute(double maxValueInclusive)
        {
            this.maxValue = maxValueInclusive;
        }

        public override bool IsValid(object value)
        {
            return value is double && (double)value <= this.maxValue;
        }
    }
}

Open in new window

0
 
LVL 11

Expert Comment

by:louisfr
ID: 40478027
Did you try RangeAttribute?
[Range(0, 199.99)]

Open in new window

0
 

Author Comment

by:ksd123
ID: 40478771
[NotGreaterThan(199.99)]  //error
   [NotGreaterThan(199.99m)]  //error
    public decimal Quantity { get; set; }

How can I specify decimal value for the custom attributes? I get compilation error "An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type"
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40478782
decimals cannot be used in this fashion. It's a limitation of the Framework. You have to use double.

e.g.

[NotGreaterThan(199.99D, ErrorMessage = "The quantity can't be greater that 199.99")]

Open in new window

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Form Data Disappears Using Back Button 4 21
Regarding swagger API 1 37
Aspx calendar pop up control 3 28
VB.net/VSTO Excel Add-in 2 18
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question