Skip to content

TryValidateModel does not work as expected #53

@icnocop

Description

@icnocop

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions