1

In ASP.NET Core 2.2 Razor Pages, I want to revalidate the model after manipulating it to make it consistent. Specifically; there are values that I do not want to get from the user or ever get sent to the user so they won't be bound from the request but will be added when the request is processing.

Model Validation happens before I change the values and ModelState.IsValid() returns false.

I am aware of TryValidateModel(object) but I don't know what object to pass into it to revalidate the entire model for a Razor page - since I'm not using MVC there's no model object being passed in the OnPost method I'm using. None of the documentation seems to indicate what I should be passing in to revalidate nor could I find any relevant docs for Core or Razor.

My code is essentially:

[...]
public class IndexModel : PageModel
{
    [BindProperty]
    public NiceModelObject SomeModelObject { get; set; } // <=== object which needs to be manipulated
    [BindProperty]
    public AnotherModelObject ObjectThatIsOk { get; set; }
    [BindProperty]
    public FanciestModelObject AnotherObjectWhatIsJustPeachy { get; set; }

    public ActionResult OnPost()
    {
        // manipulate model
        SomeModelObject.Property = ValueIWantToSetItTo;
        SomeModelObject.Property2 = ValueIWantToSetItTo2;

        // Revalidate
        ModelState.Clear();
        TryValidateModel(/*What is the object that represents my entire model in Razor?*/); // <== don't know what to put here

        if (ModelState.IsValid())
        {
                // Do Stuff, redirect
        }
        // Do other stuff, present page with error
    }

    [...]
}

Edit

Added extra items to the code to help clarify;

  • I'm aware of TryValidateModel(). If I pass SomeModelObject into it, it does not work as expected, after clearing, it contains only 1 or 2 values and SomeModelObject has at least 10 properties.
  • I'm using Razor, and unlike regular MVC the OnPost method does not have an object representing the model as a whole (that i'm aware of), so I cannot pass it to TryValidateModel().
Kruft
  • 273
  • 3
  • 13
  • 2
    Well in .NET CORE, validation is automatic, but you might want to repeat it manually. For example, you might compute a value for a property and want to rerun validation after setting the property to the computed value. In your `TryValidateModel()` pass your model `SomeModelObject` to validate. Otherwise, you can create custom validation attributes. Create a class that inherits from `ValidationAttribute`, and override the `IsValid` method. – Rahul Sharma Apr 09 '19 at 05:31
  • Possible duplicate of [Manually invoking ModelState validation](https://stackoverflow.com/questions/6360087/manually-invoking-modelstate-validation) – Moien Tajik Apr 09 '19 at 05:34
  • 1
    Building in the comment from @RahulSharma, if `SomeModelObject` represents all the values that you want to validate, pass that in to the `TryValidateModel` method. With Razor Pages, the PageModel is a kind of view model. If you want to validate multiple properties of the PageModel, pass `this` to the `TryValidateModel` method. – Mike Brind Apr 09 '19 at 09:50
  • 1
    @MoienTajik I don't think so, that refers to the regular MVC in which you would have an object which represents the entire model. – Kruft Apr 09 '19 at 21:40
  • @RahulSharma Yes, that is essentially exactly what I want, and I read that in the documentation, but like I said in the question there is no apparent object for Razor that represents the entire model. e.g. I try: `TryValidateModel(this)` and it breaks because `IndexModel` (the page's `PageModel`) has a bunch of other stuff in it (methods, properties, etc) and I'm assuming whatever `TryValidateModel` is doing when discovering Properties is breaking. – Kruft Apr 09 '19 at 21:47
  • @MikeBrind I tried to pass `this`, but it just exploded :) The stack trace is a repetition of calls to `ModelBinding.Balidation.ValidationVisitor.VisitX` methods, which looks like some sort of recursion is going on. Then an exception is eventually thrown saying `InvalidOperationException: The requested operation is invalid for DynamicMethod.` To help clarify that `SomeModelObject` is only part of the model to be validated, I added some additional code to my original post. I'll also add a bit more to make it clearer it's Razor. – Kruft Apr 09 '19 at 21:51

0 Answers0