blog.mha.dk
The on-line blog of Michael Holm Andersen

SubSonic LoadFromPost method

Monday, 29 October 2007 12:03 by mha

I'm a big fan of SubSonic and use it in almost all my Web Applications. However, yesterday I discovered yet another nice feature of SubSonic - the LoadFromPost() method. What is does is it iterate over the the Request.Forms collection and matches the name of any form element with the corresponding property for your Database Object.

This means that you can now fill your entire Object using a single line of code, like this:

Customer cust = new Customer
cust.LoadFromPost(); // Load all the properties!
cust.Save();

This will save me hours of tedious code like:

Customer cust = new Customer
cust.FirstName = txtFirstName.Text
cust.LastName = txtLastName.Text

...
...
cust.Save();

Another bonus is that I don't have to adjust my code-behind if additional columns is added to the Table (Object) - I simply rebuild the Model using SubCommander and add additional Server Controls to my form (.aspx page). It's also possible to use plain html tags, so I don't have to worry about ASP.NET adding all the extra naming container stuff, and furthermore it will work perfectly with any JavaScript or CSS that uses the id of an html tag.

Comments are closed