Solved

Custom attributes using c#

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
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…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

778 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