2

I have a Save method which gets a view model passed in. In the Viewmodel I have a bunch of custom validations that happen.

public class DealViewModel : IValidatableObject
{
    public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
    {
    }
}

public IActionResult SaveDeal(DealViewModel dealViewModel)
{
}

I want to run the validation after it hits the save method as I need to get information from session update the view model and then validate.

This is how I validate:

 var isValid = Validator.TryValidateObject(dealViewModel, context, results, true);

But my code hits the validation method before it even its in the SaveDeal method in the controller.

Is there a way to stop that and just force validate when I am ready?

1 Answer 1

0

Possible duplicate of: Correct way to disable model validation in ASP.Net Core 2 MVC

But this might help: https://learn.microsoft.com/en-us/aspnet/core/mvc/models/validation?view=aspnetcore-3.1#disable-validation

Basically, add a new class that inherits the IObjectModelValidator but does nothing on the Validate method. Also add it to the dependency injection container.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.