3

I am trying to use model validation in ASP.NET WEBAPI Core. Below mentioned is the code from my model.

[Range(typeof(decimal), "1.0", "90.1")]
public decimal price{ get; set; }

My understanding is, if I pass any value which is not in between 1.0 - 90.1, the ModelState.Valid should be false.

Here is the url I tried to call the method,

http://localhost:57270/api/testprice?price=132.7492634

Since the value which I have passed greater than 90, I was expecting ModelState.Valid as false. But always the ModelState.Valid is coming as true.

Am I missing anything? Please help?

Nkosi
  • 235,767
  • 35
  • 427
  • 472
Ane
  • 115
  • 9

2 Answers2

2

Try this:

[Range(1.0, 90.1)]
public decimal price{ get; set; }
Jack Sparrow
  • 549
  • 4
  • 13
  • Jack, Thanks for the reply. I have tried this. Its not working. I am still getting ModelState.Valid as true. – Ane Mar 03 '18 at 22:09
1

For the benefit of other users, I am posting the answer to this question.

To get full functionality of data annotations in .net core, we need to add the below line in ConfigureServices function in startup.cs file:

services.AddMvcCore().AddDataAnnotations();

Hope it helps. Have a nice day to all.

סטנלי גרונן
  • 2,917
  • 23
  • 46
  • 68
Ane
  • 115
  • 9