Solved

Custom attributes using c#

Posted on 2014-12-02
4
110 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
[X]
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
  • 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

More Than Just A Video Library

Train for your certification. Learn the latest DevOps tools. Grow your skillset to do better work.

At Linux Academy, we release new training modules every week so you'll always be up to date on the latest tech.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
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.
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

695 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