Skip to main content
Edited for improved formatiing.
Source Link
Dan Pichelman
  • 13.8k
  • 8
  • 44
  • 73

MVVM is possible in. NET 4.5 with model binding and the use of templated bound controls like FormView.

Here is the technique iI use:

I design ViewModels for each UserControl and nested UserControl and then use a FormView that is always in EditMode, where iI include the controls that bind to model properties using Binding expressions.

I set the SelectMethod and UpdateMethod of the FormView. The first returns the ViewModel and the 2nd calls TryUpdate() on it. On postbacks iI always call the FormView's Update method in Preload or Load. In that way the ViewModel is always up-to-date from the view.

I run the logic inside the ViewModel and on PreRender iI rebind the view to apply any changes. The key in this method is to inject the ViewModel into the view (UserControl) outside the view (ex. in Page level) and ofcourseof course ensure that is serializable and cached in ViewState or any other place iI choose.

Finally iI disable the ViewState in all controls since they are ultimetaly "driven" by the cached stateful ViewModel and don't need their own state.

This technique never failed me so far and iI only wish iI could find a solution for command/button binding too, like WPF.

P.S. I also observed that caching a viewmodel in ViewState gives smaller footprint on the page than having it active on individual controls. The reason is that you can not control which properties and nested elements are serialized in server controls and you have to choose all or none. Happy coding.

MVVM is possible in. NET 4.5 with model binding and the use of templated bound controls like FormView.

Here is the technique i use:

I design ViewModels for each UserControl and nested UserControl and then use a FormView that is always in EditMode, where i include the controls that bind to model properties using Binding expressions.

I set the SelectMethod and UpdateMethod of the FormView. The first returns the ViewModel and the 2nd calls TryUpdate() on it. On postbacks i always call the FormView's Update method in Preload or Load. In that way the ViewModel is always up-to-date from the view.

I run the logic inside the ViewModel and on PreRender i rebind the view to apply any changes. The key in this method is to inject the ViewModel into the view (UserControl) outside the view (ex. in Page level) and ofcourse ensure that is serializable and cached in ViewState or any other place i choose.

Finally i disable the ViewState in all controls since they are ultimetaly "driven" by the cached stateful ViewModel and don't need their own state.

This technique never failed me so far and i only wish i could find a solution for command/button binding too, like WPF.

P.S. I also observed that caching a viewmodel in ViewState gives smaller footprint on the page than having it active on individual controls. The reason is that you can not control which properties and nested elements are serialized in server controls and you have to choose all or none. Happy coding.

MVVM is possible in. NET 4.5 with model binding and the use of templated bound controls like FormView.

Here is the technique I use:

I design ViewModels for each UserControl and nested UserControl and then use a FormView that is always in EditMode, where I include the controls that bind to model properties using Binding expressions.

I set the SelectMethod and UpdateMethod of the FormView. The first returns the ViewModel and the 2nd calls TryUpdate() on it. On postbacks I always call the FormView's Update method in Preload or Load. In that way the ViewModel is always up-to-date from the view.

I run the logic inside the ViewModel and on PreRender I rebind the view to apply any changes. The key in this method is to inject the ViewModel into the view (UserControl) outside the view (ex. in Page level) and of course ensure that is serializable and cached in ViewState or any other place I choose.

Finally I disable the ViewState in all controls since they are ultimetaly "driven" by the cached stateful ViewModel and don't need their own state.

This technique never failed me so far and I only wish I could find a solution for command/button binding too, like WPF.

MVVM is possible in. NET 4.5 with model binding and the use of templated bound controls like FormView. What i do

Here is tothe technique i use:

I design ViewModels for each UserControl and nested UserControl and then use a FormView that is always in EditMode, where i include the controls that bind to datamodel properties using Binding expressions. Then i

I set the SelectMethod and UpdateMethod of the FormView. The first returns the ViewModel and the 2nd calls TryUpdate() on it. On postbacks i always call the FormView's Update method in Preload or Load. In that way the ViewModel is always up-to-date from the view. 

I run the logic inside the ViewModel and on PreRender i rebind the view to apply any changes. The key in this method is to inject the ViewModel into the view (UserControl) outside the view (ex. in Page level) and ofcourse ensure that is serializable and cached in ViewState for exampleor any other place i choose. I also

Finally i disable the ViewState in all controls since they are ultimetaly "driven" by the cached stateful ViewModel and don't need their own state. 

This technique never failed me so far and i only wish i could find a solution for command/button binding too, like WPF.

P.S. I also observed that caching a viewmodel in ViewState gives smaller footprint on the page than having it active on individual controls. The reason is that you can not control which properties and nested elements are serialized in server controls and you have to choose all or none. Happy coding.

MVVM is possible in. NET 4.5 with model binding and the use of templated bound controls like FormView. What i do is to design ViewModels for each UserControl and nested UserControl and then use a FormView that is always in EditMode, where i include the controls that bind to data using Binding expressions. Then i set the SelectMethod and UpdateMethod of the FormView. The first returns the ViewModel and the 2nd calls TryUpdate() on it. On postbacks i always call the FormView's Update method in Preload or Load. In that way the ViewModel is always up-to-date from the view. I run the logic inside the ViewModel and on PreRender i rebind the view to apply any changes. The key in this method is to inject the ViewModel into the view (UserControl) outside the view (ex. in Page level) and ofcourse ensure that is serializable and cached in ViewState for example. I also disable the ViewState in all controls since they are ultimetaly "driven" by the cached stateful ViewModel and don't need their own state. This technique never failed me so far and i only wish i could find a solution for command/button binding too, like WPF. Happy coding.

MVVM is possible in. NET 4.5 with model binding and the use of templated bound controls like FormView.

Here is the technique i use:

I design ViewModels for each UserControl and nested UserControl and then use a FormView that is always in EditMode, where i include the controls that bind to model properties using Binding expressions.

I set the SelectMethod and UpdateMethod of the FormView. The first returns the ViewModel and the 2nd calls TryUpdate() on it. On postbacks i always call the FormView's Update method in Preload or Load. In that way the ViewModel is always up-to-date from the view. 

I run the logic inside the ViewModel and on PreRender i rebind the view to apply any changes. The key in this method is to inject the ViewModel into the view (UserControl) outside the view (ex. in Page level) and ofcourse ensure that is serializable and cached in ViewState or any other place i choose.

Finally i disable the ViewState in all controls since they are ultimetaly "driven" by the cached stateful ViewModel and don't need their own state. 

This technique never failed me so far and i only wish i could find a solution for command/button binding too, like WPF.

P.S. I also observed that caching a viewmodel in ViewState gives smaller footprint on the page than having it active on individual controls. The reason is that you can not control which properties and nested elements are serialized in server controls and you have to choose all or none. Happy coding.

Source Link

MVVM is possible in. NET 4.5 with model binding and the use of templated bound controls like FormView. What i do is to design ViewModels for each UserControl and nested UserControl and then use a FormView that is always in EditMode, where i include the controls that bind to data using Binding expressions. Then i set the SelectMethod and UpdateMethod of the FormView. The first returns the ViewModel and the 2nd calls TryUpdate() on it. On postbacks i always call the FormView's Update method in Preload or Load. In that way the ViewModel is always up-to-date from the view. I run the logic inside the ViewModel and on PreRender i rebind the view to apply any changes. The key in this method is to inject the ViewModel into the view (UserControl) outside the view (ex. in Page level) and ofcourse ensure that is serializable and cached in ViewState for example. I also disable the ViewState in all controls since they are ultimetaly "driven" by the cached stateful ViewModel and don't need their own state. This technique never failed me so far and i only wish i could find a solution for command/button binding too, like WPF. Happy coding.