I am using ASP.NET Core 2.0 default model validation and it seems not working with the ModelState.IsValid
always true despite having wrong data in model.
[HttpPost("abc")]
public async Task<IActionResult> Abc([FromBody]AbcViewModel model)
{
if (!ModelState.IsValid) { return BadRequest(ModelState); }
...
}
public class AbcViewModel
{
[Required(ErrorMessage = "Id is required")]
[Range(100, int.MaxValue, ErrorMessage = "Invalid Id")]
public int Id { get; set; }
public bool Status { get; set; }
}
When I post data from Angular app, the values are mapping to model correctly but if Id
is "0" or less than 100, both the Required
and Range
validators aren't working and ModelState.IsValid
is always true. What I am missing?