Reported by a user:
The problem I am having is with models like:
// the view model
public class RegisterModel : IValidatableObject
{
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
if (somethingIsWrong)
yield return new ValidationResult("Something went wrong", new[] { "Field1" });
if (somethingElseIsWrong)
yield return new ValidationResult("Something went wrong", new[] { "Field2" });
}
}
// the controller action
public HttpResponseMessage Post(RegisterModel model)
{
return Request.CreateResponse(HttpStatusCode.OK);
}
In the controller I expect this error to be retrievable from ModelState["model.Field1"] but instead it is ModelState["model"]. Additionally, because the field name is dropped there can be only one error. If somethingIsWrong && somethingElseIsWrong only one of the errors is available.
The problem I am having is with models like:
// the view model
public class RegisterModel : IValidatableObject
{
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
if (somethingIsWrong)
yield return new ValidationResult("Something went wrong", new[] { "Field1" });
if (somethingElseIsWrong)
yield return new ValidationResult("Something went wrong", new[] { "Field2" });
}
}
// the controller action
public HttpResponseMessage Post(RegisterModel model)
{
return Request.CreateResponse(HttpStatusCode.OK);
}
In the controller I expect this error to be retrievable from ModelState["model.Field1"] but instead it is ModelState["model"]. Additionally, because the field name is dropped there can be only one error. If somethingIsWrong && somethingElseIsWrong only one of the errors is available.