Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Custom attributes using c#

Posted on 2014-12-02
4
106 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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

829 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