MVC3 Lazy Model Binding / Download

I have a standard RequiredIf attribute for some conditional validations.
My form and model are quite large, the model has 63 properties and most of them are verified.

Anyway, I have a problem when my model loads while my validation is in progress.
If I set a breakpoint in the first line of code inside the RequiredIf IsValid method, I see that my model has many null values. When I press F5 (to continue checking the next property), there are still a lot of zeros. If I press F5 about 5 times, the model is magically populated with the data I published.

My first 5-6 validations because of this wind up. Does anyone know something about how models load? I do not use custom model binding.

+4
source share
2 answers

The solution was to completely override the standard model binding. Create your own binder, download the entire model manually, and then do all the checks for your model. I'm still convinced that there is some way to just say β€œHey, load the whole model before checking out” in this standard Binder model, but I had to make my own for now.

+3
source

I would review your page and model and split the data model and look at a few small models, possibly using a few partial views. This way you can better manage your model. Imagine you are writing tests for this model. 63 properties are too many, and probably you need to turn to this, otherwise it sounds that you will encounter problems later.

+1
source

Source: https://habr.com/ru/post/1402936/


All Articles