Opinionated Input Builders for ASP.Net MVC – Part 3 the source code.

There have been a number of requests for me to provide the source for the input builders.  I consider this code to be a prototype because, I expect to change the API as this post series helps me work out what should be supported.  I have full intention to add this code to the MvcContrib project once I get the appropriate feedback and documentation as a result of fleshing out this post series.

With that being said the source code is located here: There is only V1 up there now but I expect to have some iterations pretty quickly.

http://code.google.com/p/erichexter/downloads/list?q=label:Inputbuilder

Requirements:

  • ASP.Net MVC 1.0
  • Visual Studio 2008

Solution Structure:

The solution is structured into two projects.  The InputBuilder project is what I would see providing as a dll in the future.  The web project is a MVC web application that shows how to use the InputBuilders as well as override markup for the builders.

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 .Net, Asp.Net, Asp.Net MVC, c#, CoC. Bookmark the permalink. Follow any comments here with the RSS feed for this post.
  • http://www.andrewsiemer.com Andrew Siemer

    Thanks man! Great stuff. I love that you structured this in a way that it can easily be used across projects. Wonderful.

  • http://www.lobstersoft.com Adrian Grigore

    Hi Eric,

    This looks great! I’d love to use it, but have encountered problem. (crash). Here’s what I did:

    1. Downloaded and compiled Input Builders v4
    2. Added to my Input Builders project
    3. Created a new view with just one field:

    < %=Html.Input(m=>m.SearchString)%>

    Now whenever the view is executed, I get a NullReferenceException in line 106 of InputBuilder\DefaultConventions.cs.

    The problem seems to be line 181 of ReflectionHelper.cs :

    var propertyInfo = ((MemberExpression)expressionToCheck).Member as PropertyInfo;

    This cast fails. My reflection skills are not highly evolved to tell what is going wrong though. If you need any more info, please let me know.

    Thanks,

    Adrian

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

    @adrian is your SearchString a Property or another type of member?

  • http://www.devermind.com Adrian Grigore

    Thanks for the hint, the property was indded just a model field. It works fine now.

    One other thing I noticed: Having an action method like this results in a crash:

    //
    // GET: /Search/
    [AcceptVerbs(HttpVerbs.Get)]
    public virtual ActionResult Index()
    {
    return View();
    }

    The model has to be explicitly intialized in the controller like this:

    //
    // GET: /Search/
    [AcceptVerbs(HttpVerbs.Get)]
    public virtual ActionResult Index()
    {
    return View(new ViewModel());
    }

    If the model contains any child entities, these also have to be explicitly initalized before the view can be rendered. This is certainly not a big issue, but it differs from the standard Html helper implementation.

    Also, I’ve tried converting one of my site’s forms to your implementation and mostly worked fine, but there’s one problem I don’t know how to circumvent without altering your implemetation.

    I usually have text inputs with a maximum and minimum size, for example:

    < %= Html.TextBoxFor(m => m.User.Email, new { maxlength = 80,size=50 })%>

    Maxlength could be retrieved by ModelPropertyFactory.Create from a StringLength attribute applied to the model property. But the field size cannot be guessed just by looking at the model. And I might also want to set a special class on the input field, which also does not seem to work with the current implementation.

    I guess the only workaround is coding an override that allows me to attach arbitrary input field attribute like this:

    < %=Html.Input(m=>m.SearchString, new { maxlength = 80,size=50 })%>

    But I hate branching other people’s code, especially since I assume you are going to further improve and update your implementation. Would you consider implementing this feature or adding it to your implementation if I implement it and send you the altered code?

    Thanks,

    Adrian

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

    @Adrian, I saw some issues like this coming and was really thinking of turning the modelproperty type into an object that stores some type of dictionary internally so that it was easy to extend your additional items into model property and into your partials. than rather than branching the Input method you could add extension methods that would allow you to add this additional values into the modelproperty so it would look like.

    Input(m=>m.SearchString).MaxLength(80).Size(50)

    or something like that.

    Do you prefer having these values set in your view or as an attribute on your ViewModel?

  • http://www.devermind.com Adrian Grigore

    @Eric: You’re right, the fluent syntax you propose is a bit safer than the syntax I had in mind since there would no way to misspell properties. It’s a bit more overhead because it means creating an extension method for each property, but I would definitely prefer it.

    I’d definitely prefer to have this in the view, because this gives me more flexibility. Also, specifying a strongly view-related property such as a CSS class with model attributes would feel a bit odd.

  • http://www.devermind.com Adrian Grigore

    Oh, one thing I forgot: In the case of MaxLength, I would actually prefer Html.Input to figure this out by itself by looking at the StringLength attribute applied to the model property.