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 passSomeModelObject
into it, it does not work as expected, after clearing, it contains only 1 or 2 values andSomeModelObject
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()
.