9

Hi I have following in my Asp.net MVc Model

TestModel.cs

public class TestModel
{      
public double OpeningAmount { get; set; }

[Required(ErrorMessage="Required")]
[Display(Name = "amount")]
[Range(0 , double.MaxValue, ErrorMessage = "The value must be greater than 0")]
public string amount { get; set; }

}

Now from my controller "OpeningAmount " is assign .

Finaly when I submit form I want to check that "amount" must be greater than "OpeningAmonut" . so want to set Range dynamically like

[Range(minimum = OpeningAmount , double.MaxValue, ErrorMessage = "The value must be greater than 0")]

I do not want to use only Jquery or javascript because it will check only client side so possible I can set Range attribute minimum dynamically than it would be great for.

5
  • 1
    Data written in attributes are "constant" unless you write your own Custom Metadata Provider, where you can "adjust" these values. Commented Jan 9, 2014 at 13:37
  • 1
    Check stackoverflow.com/questions/5382129/… Commented Jan 9, 2014 at 14:05
  • @Murali I have gone through this link but I don't think it could be useful to me? Commented Jan 9, 2014 at 16:37
  • @Dilip0165, Ok. I am removing it.. Thank you :) Commented Jan 9, 2014 at 16:39
  • @Murali I don't mean it . keep as it is . May be it could be helpful to someone Commented Jan 9, 2014 at 17:11

2 Answers 2

10

Recently there's been an amazing nuget that does just that: dynamic annotations and it's called ExpressiveAnnotations

It allows you to do things that weren't possible before such as

[AssertThat("ReturnDate >= Today()")]
public DateTime? ReturnDate { get; set; }

or even

public bool GoAbroad { get; set; }
[RequiredIf("GoAbroad == true")]
public string PassportNumber { get; set; }
Sign up to request clarification or add additional context in comments.

3 Comments

can you please tell me when to use AssertThat and when to use RequiredIf using ExpressiveAnnotations library?
@Thomas Use AssertThat when you want to write business logic validation on the property you're decorating RequiredIf is a different thing. Use it when you have a property that sometimes is Required to have data (not left empty with no value) while sometimes else it can be empty. The logic of when it's required or not is what you write inside the RequiredIf.
can you please post a sample which guide me when to use AssertThat and when to use RequiredIf using ExpressiveAnnotations library. thanks
2

There's no built-in attribute which can work with dependence between properties.

So if you want to work with attributes, you'll have to write a custom one.

Se here for an example of what you need.

You can also take a look at dataannotationsextensions.org

Another solution would be to work with a validation library, like (the very nice) FluentValidation .

3 Comments

I will try it and let you know
I prefer your first link in which customvalidation is provided . Thanks for your support
Link to dataannotationextensions.org is dead. Do you know where moved or if they still exist?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.