问题
If you know the MVVM pattern for WPF then you know Josh smith msdn article where a CustomerViewModel does not hold a simple property like:
public string FirstName {get;set;}
Rather a ViewModel wraps a Model and delegates the property access like this:
public string FirstName
{
get { return _customer.FirstName; }
set
{
if (value == _customer.FirstName)
return;
_customer.FirstName = value;
base.OnPropertyChanged("FirstName");
}
}
I have not seen this in asp.net mvc. Is this due to the missing INotifyPropertyChanged interface?
回答1:
I have not seen this in asp.net mvc
That's normal. You shouldn't see it. MVC is a different pattern than MVVM. In MVC the view has nobody to notify of any changes. The MVVM pattern is not adapted to the stateless nature of the web.
回答2:
The ViewModel and Model pieces from MVVM have a different definition than when used in MVC
In MVVM, the ViewModel is your application, while the View just provides a user-friendly interface for it. In MVC, the View is your application, the ViewModel provides data for it, and the Controller handles application flow and logic.
The Models are also different between the two patterns. In MVC, the M represents both data models and view models, while in MVVM the M only represents data models.
To summarize, MVC's M+C is equal to MVVM's VM, and MVC's M contains a mix of both MVVM's M and VM pieces
As a side note, the INotifyPropertyChanged interface is used by WPF to automatically update the UI when a property changes. This sort of thing is not used in MVC, so not needed.
来源:https://stackoverflow.com/questions/11213644/is-the-viewmodel-in-asp-net-comparable-to-the-viewmodel-in-wpf