janPavlik janPavlik - 1 month ago 6
C# Question

How can I call the viewmodel's method with data of the control in DOTVVM?

I have .dotcontrol and I need to call function in viewmodel with data from control.

The method cannot be in the control, because it needs facade which is available only in viewmodel. So I declared interface for viemodel and implemented this function.

But if I call it like below:

<dot:Button Click="{controlCommand: ViewModelFunction(ControlProperty)}" Text="CHECK CODE"/>


it causes the following error:


The Name 'ViewModelFunction' does not exist in the current context'.


So I tried call it just with command:

<dot:Button Click="{command: ViewModelFunction(ControlProperty)}" Text="CHECK CODE"/>


This looked fine, but if I have two controls on one page, it doesn't work. Because the method is called with data of first usage of control. Data of another usage of the control is not recognized.

How can I call the viewmodel's method with data of the control?

Answer

The controlCommand does not work because the ViewModelFunction is declared on ViewModel and controlCommands default binding context is the control. You can use the _this parameter to access method and properties of view model. So it should look like:

<dot:Button Click="{controlCommand: _this.ViewModelFunction(ControlProperty)}" Text="CHECK CODE"/>

By the way, we are planing to remove this limitation of command bindings, so the second approach will hopefully work in the future.