Validating date range in asp net
I have used Switch statement here to check which Validation Type the user is requested for. Here Validation Result represents a container for the results of a validation request.Is Valid method above returns the Validation Result which is a member of System. Now with these pieces of code our Date Validation Attribute class is ready to consume. Now lets come to my model class where I am going to use this as Data Annotation Attribute.Figure 1: Test Application in Use The Code The code is very simple and does not require much of an explanation.
The method shown may be of use if you have an application that receives dates as strings rather than as actual Date Time values.
I understand that there are lots of tight coupling with the implementation class with this Validation component, this code is just for indicative usage of the Custom Data Annotation an extensibility feature provided by ASP. In this example I have created a Account class and declared just two properties one is To Date and another is From Date for this example.
In the code above I am keeping the form method as post and Action to my Date Validation Action of Home Controller.
This reduces the amount of code you need to write and makes the code you do write less error prone and easier to maintain. NET MVC and Entity Framework Code First is a great example of the DRY principle in action.
NET MVC encourages you to specify functionality or behavior only once, and then have it be reflected everywhere in an application.