ViewModels in ASP.NET
I got a couple of questions about ViewModels in ASP.NET; and my conscience has stirred me to action. I was aiming to finish the immensely cool project that we call GaffrServer and I want to use the ASP.NET viewmodels to make that happen.
Side note on what GaffrServer is: Imagine that you prototype in Gaffr.net using the upcoming Application WPF framework WECPOF (oh I just love inventing code names for our projects: WECPOF - ECO + WPF – get it?), and you want persistence. You can write to a XML file, or set up a database… But you will also be able to connect to GaffrServer – GaffrServer is cloud service that you upload your model to, then you get back an url that is the up and running server side persistence mapper with the synchronization service running – WOW! I WANT IT NOW.
Anyway the GaffrServer project is sort of dragging its feet so I am better of just showing ASP.NET ViewModels.
I wrapped up a sample that looks like this:
Notice the derived attributes on OrderItem and Order:
(Note that the TotalCost actually use the derivation in CostForRow – super cool imho)
Then I did a ViewModel:
I know some of you might initially frown at the complexity, but trust me, you just click away and use the suggested columns that are available on the context menu – I seldom have a specific plan when I start – I think and do at the same time. That is the hallmark of a tool that is helping you…
The basic idea was to fill in an order, add OrderLines, set the correct Article on the OrderLine, see the price per row, and the total price, taking into account what delivery type you use…
There you go, a neat little list of requirements so easily said – and easily implemented.
I created a ECO ASP.NET application from the ECO Solution Wizard, Added the scriptmanager and an UpdatePanel to get Ajax behavior.
Then I add the ViewModelASPNET component, hook it up to the EcoSpaceManager, and tell it to use ViewModel ViewModelOrder:
I need to write some code to get test data and to hook up the ViewModelASPNET control to an order:
I also must init the viewmodels before they are used:
Then I get something that looks like this:
Notice the crazy style on CostForRow, the style on TotalCost and on Selected orderitem – I get these by providing a css stylesheet that has styles using the names I set in the ViewModelColumns StyleRef property…
As you change deliverytype or anything else the TotalCost will be updated on the next postback.
In this demo I chose to use the setting ViewModelASPNET.PostbackOnComboboxChange="true"
I also chose to use the setting ViewModelASPNET.GridSizeFixedWithScrollbar="True"
The bits to run this is in the eco build after this date, it is in the demos called EcoASPNETWithViewModel