2 Replies - 3933 Views - Last Post: 12 February 2011 - 01:25 PM

#1 jbeckton  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 14-January 11

Custom Data Annotations for MVC

Posted 19 January 2011 - 05:58 PM

I am trying to create some custom model validation by using Data Annotations in an MVC application.

The problem is I have instances where one property is required but only based on the value of another property. I figured that I would need to write a custom attribute class for this but I cannot find any examples other the most basic that do not address my situation.


for example...

[Required]
public bool FeeAgree
		{
		get
		{
			return _feeAgree;
		}
		set
		{
			_feeAgree = value;
		}
		}

public decimal Fee
		{
		get
		{
			return _fee;
		}
		set
		{
			_fee = value;
		}
	}



in the above snippet, the FeeAgree property is only required if the Fee property value is > 0

Is This A Good Question/Topic? 0
  • +

Replies To: Custom Data Annotations for MVC

#2 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4429
  • View blog
  • Posts: 7,698
  • Joined: 08-June 10

Re: Custom Data Annotations for MVC

Posted 19 January 2011 - 06:45 PM

Moved to the ASP.NET MVC forum.
Was This Post Helpful? 0
  • +
  • -

#3 Wimpy  Icon User is offline

  • R.I.P. ( Really Intelligent Person, right? )
  • member icon

Reputation: 159
  • View blog
  • Posts: 1,038
  • Joined: 02-May 09

Re: Custom Data Annotations for MVC

Posted 12 February 2011 - 01:25 PM

You can always inherit the ValidationAttribute and override the IsValid-method. :)

Example:
public class MyIsNotNullValidationAttribute : ValidationAttribute
{
    public override bool IsValid(object value)
    {
        return value != null;
    }
}



Hope it helps! :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1