[view models / page structuring series] DotVVM Context null when instantiating MarkupControl view model at PostBack in command handler

Well, for reasons of not complicating things further and make view model for the ModalDialog that is inside a MarkupControl, I’ve decided to work directly with the MarkupControl view model… nullifying it when needed (see Simple validation crashing on just one property (+other validation issues) on discussion about good practices when doing data validations with nested page sections; also this issue is a follow up of switching to nullifying the view models of the sections).

How I open the popup (popup means a MarkupControl containing a ModalDialog control and this arrangement has to stay like this otherwise again other complications and more code :face_with_peeking_eye:):
image

When I try to open the popup, it crashes here, inside OperationInstance, with Context being null:
image

:question: 1: how do I make the Context available in this scenario?
:question: 2: what do I risk with this structural approach and is it problematic?

1 Like

The context is assigned automatically to the view models just before the Init phase, so if you create the nested model later than in the constructor, you have to assign the context yourself. Just do new(clientHelper, ...) { Context = Context }

(PS: yes it was a mistake to not require the context in the DotvvmViewModelBase constructor, but we are now not going to break every single view model because of this.)

No, this shouldn’t be problematic.

1 Like

:relieved: (at least 20 chars)