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

Related Articles:

Post Footer automatically generated by Add Post Footer Plugin for wordpress.

About Eric Hexter

I am the CTO for QuarterSpot. I (co)Founded MvcContrib, Should, Solution Factory, and Pstrami open source projects. I have co-authored MVC 2 in Action, MVC3 in Action, and MVC 4 in Action. I co-founded online events like mvcConf, aspConf, and Community for MVC. I am also a Microsoft MVP in ASP.Net.
This entry was posted in Uncategorized. Bookmark the permalink. Follow any comments here with the RSS feed for this post.
  • http://16randombytes.blogspot.com Bill Barry

    :-)

    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 (http://jcheng.wordpress.com/2007/08/23/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.

  • http://weblogs.asp.net/meligy Mohamed Meligy

    Is this somewhere on codeplex yet ? :-)

  • http://www.lostechies.com/members/erichexter/default.aspx erichexter

    @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.

  • http://jakescott.tumblr.com Jake Scott

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

  • Tad

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

  • http://www.lostechies.com/members/erichexter/default.aspx erichexter

    @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.

  • http://www.lostechies.com/members/colinjack/default.aspx colinjack

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