Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
In MVVM I can clearly understand communication between View and ViewModel using INotifyPropertyChanged and Commands.

But what about ViewModel to Model communication and vice versa. For me it is unclear what approach is best for MVVM architecture.

Should I just call Model methods from ViewModel directly? And how I shoud track changes in Model - any standart approach?

There is rly little information about Model to ViewModel communication

What I have tried:

Right now I'm just directly calling Model methods from ViewModel. Tried to implement INotifyPropertyChanged on Model but that feels messy
Posted
Updated 6-Mar-18 0:12am
v2

There is nothing wrong in calling directly methods of the Model from the ViewModel.
But the Model shall have no knowledge of the ViewModel.

In order to receive events from the model without breaking the MVVM principle
you could use a so-called Event Aggregator as described here:

Using the Event Aggregator Pattern to Communicate Between View Models - TechNet Articles - United States (English) - TechNet Wiki[^]
 
Share this answer
 
The model does nothing more than get/save/or delete data from the data source. The view should never directly call any method or refer to any property in the model.

There are many ways to manage this interaction, but presenting a reasonably complete example in Q/A is beyond the intent of Q/A.

Try this youtube lesson:

C# WPF UI Tutorials: 03 - View Model MVVM Basics - YouTube[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900