2

I use asp.net mvc5, api conrollers.

I'm try valid data

[Required] [RegularExpression(@"\+?[0-9]{7,15}", ErrorMessage = "Invalid Phone Number")] public string PhoneNumber { get; set; }

And it work nice , but if I try send invalid data, server return me 2 errors

{
  "message": "The request is invalid.",
  "modelState": {
    "model.ParentPhoneNumber": [
      "Invalid Phone Number",
      "Invalid Phone Number"
    ]
  }
}    

in controller

 public async Task<IHttpActionResult> Somemethod(SomeBindingModel model)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState); // **return 2 errors**
            }


        }

How I can fix this bug and have only one validation error?

  • what exactly looks the invalid data? Is this empty string? – Leszek P May 12 '17 at 11:34
  • No matter what validation. the regular expression, min/max length, etc. if model not valid server return two errors, but must return one. – Vova Leskiv May 12 '17 at 11:38
  • @VovaLeskiv, Debug the code and inspect the ModelState before it is return. back track to see if you have any action filters that may be adding the additional errors – Nkosi May 12 '17 at 11:57
  • @VovaLeskiv Did you found the problem ? I got the same problem with duplicate modelstate errors – Emil Aug 29 '17 at 08:11
  • @Emil No. It was a not critical problem. – Vova Leskiv Sep 16 '17 at 22:21

0 Answers0