Los Techies : Blogs about software and anything tech!

Opinionated Input Builders – Part 8 the Auto Form


Once I defined what my the opinions for rendering each data type of my model as a form element it was a pretty trivial exercise to take this to the next level and render a default form for the model.  Just a single like in the view that walks over each property of the model and decides how it should render each property. Although this was not my goal of the builders it was a logical step to go from a pattern for each property to just enumerating all the properties.  I think this really shows the power of having the strongly typed view models.

While I like this approach I would not stick to this as the only way to develop forms.  I would use an approach like this to take care of my initial CRUD type screens and allow this to quickly get my views hooked up to my controllers.  The next step is to have a clear and simple path for dealing with the case where the AutoForm does not work for a scenario.  Using some of the View Templates it is pretty trivial to generate a view or a partial that displays the view code to define each input individually so that they can be re ordered or have properties overridden. 

 

Here is the form that is generated as well as the single line of view code that is used to create it.

image

 

Just to show how trivial it was to implement this functionality here is the section of code that is called to loop over each property of the model.  It would be pretty easy to modify this for your conventions to say add a Validation Summary to the top of the form.  This code simply loops of the properties of the Model Type and then renders the partial needed for each of the properties type.  Than a Submit Button is added and the form tag is closed.  Pretty simple.

image

Kick It on DotNetKicks.com
Posted Jun 17 2009, 01:38 PM by erichexter

Comments

Opinionated Input Builders ??? Part 8 the Auto Form - Eric Hexter - wrote Opinionated Input Builders ??? Part 8 the Auto Form - Eric Hexter -
on 06-17-2009 1:47 PM

Pingback from  Opinionated Input Builders ??? Part 8 the Auto Form - Eric Hexter -

Opinionated Input Builders for ASP.Net MVC - Part 2 Html Layout for the Label - Eric Hexter - wrote Opinionated Input Builders for ASP.Net MVC - Part 2 Html Layout for the Label - Eric Hexter -
on 06-17-2009 1:51 PM

Pingback from  Opinionated Input Builders for ASP.Net MVC - Part 2 Html Layout for the Label - Eric Hexter -

Bill Barry wrote re: Opinionated Input Builders – Part 8 the Auto Form
on 06-17-2009 3:00 PM

:-)

btw, you could pretty easily add in sorting to put the input elements in order:

public class MyModel {

   [Sort(1)]

   [Required]

   public string Name {get;set;}

...

}

then in the loop do a Schwartzian Transform sorting by the value of the Sort property (jcheng.wordpress.com/.../schwartzian-transform-in-c-30).

Note: Currently your model will be rendered in the order which it is defined in source, but this is not guaranteed by the compiler or by the semantics of the GetProperties method.

Mohamed Meligy wrote re: Opinionated Input Builders – Part 8 the Auto Form
on 06-18-2009 2:02 AM

Is this somewhere on codeplex yet ? :-)

Sanjeev Agarwal wrote Daily tech links for .net and related technologies - June 16-18, 2009
on 06-18-2009 2:31 AM

Daily tech links for .net and related technologies - June 16-18, 2009 Web Development Revised: ASP.NET

Reflective Perspective - Chris Alcock » The Morning Brew #371 wrote Reflective Perspective - Chris Alcock » The Morning Brew #371
on 06-18-2009 3:23 AM

Pingback from  Reflective Perspective - Chris Alcock  » The Morning Brew #371

DotNetShoutout wrote Opinionated Input Builders – Part 8 the Auto Form - Eric Hexter - Los Techies
on 06-18-2009 5:16 AM

Thank you for submitting this cool story - Trackback from DotNetShoutout

erichexter wrote re: Opinionated Input Builders – Part 8 the Auto Form
on 06-18-2009 9:10 AM

@Bill I need to get the ordering of the Properties into a convention.  That will allow the opinions to be set and modified if you opinions differ from mine. Absolutely agree that a dertminisitic way of order the properties needs to be addressed.

@Mohamed.  The source is located in Part 3.  I have not yet committed this to codeplex, I am still getting feedback but the source is available and I expect to still make some changes to how it works.

links for 2009-06-18 « dstelow notes… wrote links for 2009-06-18 « dstelow notes…
on 06-18-2009 7:29 PM

Pingback from  links for 2009-06-18 «  dstelow notes…

Jake Scott wrote re: Opinionated Input Builders – Part 8 the Auto Form
on 06-19-2009 6:43 PM

This is awesome, it is so cool that there is soo much action happening in the .net web development space!

Tad wrote re: Opinionated Input Builders – Part 8 the Auto Form
on 06-26-2009 8:22 AM

Hi, I'm interested if it will work on Mono platform, any thoughts?

erichexter wrote re: Opinionated Input Builders – Part 8 the Auto Form
on 06-26-2009 10:55 AM

@Tad, I do not know how much of the C# 3.0 expression syntax is available.  I would give it a try and see what happens.  Let me know I would be interested in learning more about what are the problems with porting this.

colinjack wrote re: Opinionated Input Builders – Part 8 the Auto Form
on 06-29-2009 5:30 AM

@erichexter

Any idea as to the schedule for getting this into MVC Contrib?

Eric Hexter wrote Opinionated Input Builders – Part 9 override the default Date Time picker
on 06-30-2009 9:07 AM

  Part 1 – Overview Part 2 – the Labe l Part 3 – the Source Code Part 4 – the Partial View Part

Daily Links for Thursday, July 2nd, 2009 wrote Daily Links for Thursday, July 2nd, 2009
on 07-02-2009 7:36 AM

Pingback from  Daily Links for Thursday, July 2nd, 2009

Opinionated Input Builders for ASP.Net MVC using partials ??? Part 1 - Eric Hexter - wrote Opinionated Input Builders for ASP.Net MVC using partials ??? Part 1 - Eric Hexter -
on 07-23-2009 2:00 AM

Pingback from  Opinionated Input Builders for ASP.Net MVC using partials ??? Part 1 - Eric Hexter -

Copyright Los Techies 2008, 2009. All rights reserved.
Powered by Community Server (Commercial Edition), by Telligent Systems