FluentValidation.AutoValidation
FluentValidation.AutoValidation copied to clipboard
`TryValidateModel` does not work as expected
Hi.
When integrating with ASP.NET Core MVC, a call to TryValidateModel does not work as expected.
public class Test1Controller : Controller
{
public ActionResult Action1(Action1ViewModel viewModel)
{
// re-validate model
this.ModelState.Clear();
this.TryValidateModel(viewModel);
if (!this.ModelState.IsValid)
{
return this.View(viewModel);
}
...
}
}
In my actual scenario, ModelState.Entries is 18 and ModelState.IsValid is false when the action method is called, as expected.
ModelState.Clear() will set Entries to 0 and IsValid to true, as expected.
When using FluentValidation.AspNetCore, a call to TryValidateModel(viewModel); will re-evaluate the model and set ModelState.Entries to 18 and ModelState.IsValid to false, for example.
I expected SharpGrip.FluentValidation.AutoValidation to do the same, but Entries is 0 and IsValid is true, as if the view model was not re-evaluated/validated, which is unexpected.
Thank you.
Hi @icnocop, thanks again :). Is this one also related to https://github.com/SharpGrip/FluentValidation.AutoValidation/pull/52?
Hi.
I would classify this as a different issue, not directly related to #52, but it may require #52 so the code gets far enough to be able to call TryValidateModel in a real scenario.
Thank you.