Solved

Custom attributes using c#

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

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 74

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

747 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now