[RESOLVED]Applying Multiple DataAnnotaion using MVC

I want to apply multiple data annotation on a property named Year.

Following is my code:

[Required(Error Message="Year is Required")]
[Range(1900, 2050, ErrorMessage = "Enter the Valid Year")]
public string Year

When i enter an invalid year then it is working fine but when i do not enter any year in the textbox , it is again showing the Range error but the expected error message is :"Year is Required".

Please Help..

Hi Nimit,

If above is exact to your program code. I think 

[Required(ErrorMessage="Year is Required")]

is correct Format. ErrorMessage is one word

hope this helps

Oops my bad. I have applied the same code without space in ErrorMessage. The problem still exists

Have you tried something like

[Range(1900,2050,ErrorMessage = "{0} is a valid entry, please restrict your entry to between {1} and {2}")]

to see if that would fix it?

Hi Nimit Joshi,

Thanks for your post.

As community member said,without space in ErrorMessage. and I tried your code,it worked on my side,you try add ValidationSummary in the view,like this:

@using (Html.BeginForm())
{
        @Html.ValidationSummary()
      //some code
              
}

Hope this can be helpful.

Best Regards,

Eileen

Leave a Reply