Los Techies : Blogs about software and anything tech!

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

Kick It on DotNetKicks.com
Posted Jun 10 2009, 09:36 AM by erichexter
Filed under: , , , ,

Comments

DotNetShoutout wrote Opinionated Input Builders for ASP.Net MVC – Part 3 the source code. - Eric Hexter -
on 06-10-2009 2:42 PM

Thank you for submitting this cool story - Trackback from DotNetShoutout

Opinionated Input Builders for ASP.Net MVC ??? Part 3 the source … wrote Opinionated Input Builders for ASP.Net MVC ??? Part 3 the source …
on 06-10-2009 3:22 PM

Pingback from  Opinionated Input Builders for ASP.Net MVC ??? Part 3 the source …

Andrew Siemer wrote re: Opinionated Input Builders for ASP.Net MVC – Part 3 the source code.
on 06-10-2009 10:02 PM

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

Daily Links for Wednesday, June 10th, 2009 wrote Daily Links for Wednesday, June 10th, 2009
on 06-10-2009 10:05 PM

Pingback from  Daily Links for Wednesday, June 10th, 2009

Eric Hexter wrote Opinionated Input Builders for ASP.Net MVC – Part 4 the Partial View Inputs
on 06-10-2009 11:02 PM

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

Opinionated Input Builders for ASP.Net MVC ??? Part 3 the source code. - Eric Hexter - wrote Opinionated Input Builders for ASP.Net MVC ??? Part 3 the source code. - Eric Hexter -
on 06-10-2009 11:05 PM

Pingback from  Opinionated Input Builders for ASP.Net MVC ??? Part 3 the source code. - 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-10-2009 11:06 PM

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

Jeffrey Palermo (.com) wrote Input Builders for ASP.NET MVC series continues from Eric Hexter
on 06-10-2009 11:48 PM

Eric Hexter is continuing his multi - part series on Opinionated Input Builders . He’s on a role! They are expression-based, meaning they eliminate the need for most strings for Html Helpers in your views. Part 1 – Overview Part 2 – the Labe l Part 3

Opinionated Input Builders for ASP.Net MVC ??? Part 5 the Required input - Eric Hexter - wrote Opinionated Input Builders for ASP.Net MVC ??? Part 5 the Required input - Eric Hexter -
on 06-11-2009 6:53 AM

Pingback from  Opinionated Input Builders for ASP.Net MVC ??? Part 5 the Required input - Eric Hexter -

Daily Links for Thursday, June 11th, 2009 wrote Daily Links for Thursday, June 11th, 2009
on 06-11-2009 8:02 AM

Pingback from  Daily Links for Thursday, June 11th, 2009

Eric Hexter wrote Opinionated Input Builders Part 6 – Performance of the builders
on 06-13-2009 1:15 PM

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

Eric Hexter wrote Opinionated Input Builders - Part 7 More on Performance / Take 2.
on 06-14-2009 3:15 PM

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

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

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

Adrian Grigore wrote re: Opinionated Input Builders for ASP.Net MVC – Part 3 the source code.
on 06-28-2009 3:50 AM

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

erichexter wrote re: Opinionated Input Builders for ASP.Net MVC – Part 3 the source code.
on 06-28-2009 12:20 PM

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

Adrian Grigore wrote re: Opinionated Input Builders for ASP.Net MVC – Part 3 the source code.
on 06-29-2009 8:53 AM

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

erichexter wrote re: Opinionated Input Builders for ASP.Net MVC – Part 3 the source code.
on 06-29-2009 9:20 AM

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

Adrian Grigore wrote re: Opinionated Input Builders for ASP.Net MVC – Part 3 the source code.
on 06-29-2009 10:02 AM

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

Adrian Grigore wrote re: Opinionated Input Builders for ASP.Net MVC – Part 3 the source code.
on 06-29-2009 10:04 AM

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.

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

Summary 11.06.2009 – 02.07.2009 « Bogdan Brinzarea’s blog wrote Summary 11.06.2009 &ndash; 02.07.2009 &laquo; Bogdan Brinzarea&#8217;s blog
on 07-02-2009 8:17 AM

Pingback from  Summary 11.06.2009 – 02.07.2009 «  Bogdan Brinzarea’s blog

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