Los Techies : Blogs about software and anything tech!

Opinionated Input Builders for ASP.Net MVC using partials – Part 1


It has taken a while to really understand how different pieces and ideas can fit together to give a concise and productive form input helpers for the asp.net mvc framework. I have pulled together this idea from the following sources:

Now that I have given credit to everyone who has helped me get to the point of understanding how to pull of these pieces together.  

The Model comes first.

The goal of these control helpers is to reward you for developing MVC with the Model first.  Yeah there is a reason that Model View Controller starts with the Model. Using the strongly typed views in the aspx view engine we can carry the type down to the control helpers with intellisense and then build html input control based on conventions for rendering specific CLR types to specific HTML output.  Now my biggest problem with the ways that this has been attempted to date is that once helpers started to take on more mark up beyond the < input > tag it was hard to modify as that markup ended up being written in code rather than in a view file.  This is where I abstracted the mark up and the logic to decide which markup to render so that there is a solution that is easy to maintain the markup and it is easy to add new conventions or change the conventions for how a particular type or model property is rendered to a control.

First here is an example of a model rendered in a strongly typed view and the markup used to create this.  There are attributes applied to the model to add some specific control over how the UI is rendered. It is important to call out that in this case I have built a (view) Model specifically to represent a single view.  I do not intend to reuse this model in another views.  I think trying to get reuse out of models is a mistake in most circumstances, it is better to keep your model clean so that it represents exactly what you want for the View.  Each of these attributes are used by my conventions to decide which Partial View to render.

image

 

This is the example of the view page which is rendering the view and the markup used to create this.  The Html.Input uses the LamdaExpression syntax to declare which property needs and input. 

image

It is important to call out that the Guid property is rendered as a Hidden input tag and that is why it does not show up in the UI. This is a model element that is used to carry state in the form between posts.

Walkthrough of the Html.Input( ) method

The syntax for calling into the input method uses a lamda expression like so…..  This has full intelisense support.

image

 

The helper determines that since the Name is a string data type it decides by convention to render this property using the String.aspx partial view.

image

Here is the markup of the String.aspx partial view.  As you can see it uses a Master Page to control how its label and input is rendered.

image

Here is the Field.Master Master Page this controls the layout of the input, label, example text, and validation message.

 

 

 

 

 

 

image

 

More to come soon.. What do you like… what do you dislike?

Kick It on DotNetKicks.com
Posted Jun 09 2009, 12:21 PM by erichexter

Comments

Matt Hinze wrote re: Opinionated Input Builders for ASP.Net MVC using partials – Part I
on 06-09-2009 2:06 PM

Eric it looks awesome.  I love the templated partial views per input.  That's really important for a maintainable design. One though is that I do want the option to not use a view but to render a string or do something else.  I don't like the HtmlHelper at all, and I like to avoid it if possible. I'd stick to more like how we've done it, but with a default to the templated partial.

erichexter wrote re: Opinionated Input Builders for ASP.Net MVC using partials – Part I
on 06-09-2009 2:43 PM

@Matt Hinze,, I was really trying to avoid having a base class or using the this.Input syntax.  That left me with making the entry point being a HtmlHelper extension method.  The extension method is really just an entry point to get to the builder.  it just instantiates the input builder.

Matt Hinze wrote re: Opinionated Input Builders for ASP.Net MVC using partials – Part I
on 06-09-2009 3:43 PM

Just curious, why are you trying to avoid having a base class?

joe wrote re: Opinionated Input Builders for ASP.Net MVC using partials – Part I
on 06-09-2009 3:53 PM

I really like the templated partials.  Does the [Partial] attribute exist to render partial other than your specified by your conventions, or is it for something else?   Also, is the source available somewhere?  

erichexter wrote re: Opinionated Input Builders for ASP.Net MVC using partials – Part I
on 06-09-2009 4:43 PM

@Matt I am trying to avoid a base class because you only get one.  The same way I do not want my persistence framework to force a base class on my domain model.  I would hate to force a base class on my views because of one of my control rendering extensions.

erichexter wrote re: Opinionated Input Builders for ASP.Net MVC using partials – Part I
on 06-09-2009 4:55 PM

@joe source code to come soon...

Eric Hexter wrote Opinionated Input Builders for ASP.Net MVC - Part 2 Html Layout for the Label
on 06-09-2009 9:26 PM

  In part two of this series I will cover the different components of the Input as it is rendered

Jeffrey Palermo (.com) wrote Input Builders for ASP.NET MVC in the works
on 06-09-2009 11:20 PM

Subscribe to my feed here: feeds.jeffreypalermo.com/jeffreypalermo Eric Hexter has started a multi - part series on Opinionated Input Builders .  They are expression-based, meaning they eliminate the need for most strings for Html Helpers

Reflective Perspective - Chris Alcock » The Morning Brew #365 wrote Reflective Perspective - Chris Alcock &raquo; The Morning Brew #365
on 06-10-2009 1:39 AM

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

Mihai wrote re: Opinionated Input Builders for ASP.Net MVC using partials – Part I
on 06-10-2009 2:03 AM

I find the idea of having an attribute like [PartialView("")] on my domain model awful, I'd much rather configure the input builder externally.

Anyways it's an approach, but one that doesn't fit my taste at all.

Opinionated Input Builders for ASP.Net MVC using partials ??? Part I … wrote Opinionated Input Builders for ASP.Net MVC using partials ??? Part I &#8230;
on 06-10-2009 2:39 AM

Pingback from  Opinionated Input Builders for ASP.Net MVC using partials ??? Part I …

Maarten Balliauw wrote re: Opinionated Input Builders for ASP.Net MVC using partials – Part I
on 06-10-2009 3:48 AM

I'm voting this for ASP.NET MVC 2.0 :-)

Stephen Young wrote re: Opinionated Input Builders for ASP.Net MVC using partials – Part I
on 06-10-2009 4:52 AM

I thought one of the drivers behind MVC is to seperate your view from the model, you have just created a dependancy cycle between your view and your model.

I think this defeats the whole point behind MVC.  The approach you use is fine if it is used on a DTO (data transfer object) that is returned from the controller for use by a specific view.  It should NEVER be used on your actual domain classes.

Andre Sachs wrote re: Opinionated Input Builders for ASP.Net MVC using partials – Part I
on 06-10-2009 6:31 AM

Erm you have polluted your model with layout details, not a good start :)

erichexter wrote re: Opinionated Input Builders for ASP.Net MVC using partials – Part I
on 06-10-2009 6:57 AM

@Stephen Young. I totaly agree with you. I guess I did not make the opinion clear that the Model I am using here is specificly a View Model for one specific view.  I have used a map to go from my domain to my view Model.  I would never suggest putting any attributes from any framework on my domain model.  I do not want any layer forcing that on the core of my system.

I do have a way of implementing all of these attributes in the view by chaining methods on the Input( ) method itself.  Sounds like I need to have a post.

Stephen Young wrote re: Opinionated Input Builders for ASP.Net MVC using partials – Part I
on 06-10-2009 8:48 AM

@erichexter  Excellent I would love to see how you do the chaining, I really think the basic concept is great as long as it is targeted at a view model.  We do something very similar in our current architecture (although it has home spun MVC as we started before ASP.Net MVC was around)

I have found in general unless you have a fairly trivial domain that doesn't do much more than basic CRUD operations it becomes unwieldy to deal directly with a domain model inside your views.  We have use case specific models (these usually map to a single view or a couple of closely related views) and map from the domain to the use case/view model.  

Regarding base classes, I share your aversion.

Steve Smith's Blog wrote Binding in ASP.NET MVC
on 06-10-2009 11:30 AM

At my ASP.NET MVC + SOLID Principles talk in Cleveland last night, I had a couple of questions about binding in ASP.NET MVC.  For instance: Can you still do something like <%= Bind(“Foo”) %> in your form? How does the controller that receives

DotNetShoutout wrote Opinionated Input Builders for ASP.Net MVC using partials – Part I - Eric Hexter -
on 06-10-2009 12:01 PM

Thank you for submitting this cool story - Trackback from DotNetShoutout

Duncan Godwin wrote re: Opinionated Input Builders for ASP.Net MVC using partials – Part I
on 06-10-2009 2:16 PM

I like the idea.  I'm wondering how far this could go towards Dynamic Data and display all fields using one method based on the view model.  This would save adding a property to the view model and then updating the view.  The form generating method could take some paramaterisation for the one input helper that is different.

erichexter wrote re: Opinionated Input Builders for ASP.Net MVC using partials – Part I
on 06-10-2009 2:29 PM

@Duncan, If you look at the source code I have a method call InputForm() which generates the entire form from a single method call.  So I will have a post to show what that does.  I did not want to start with that as it might take a lot of explaining of how all the magic took place, to get to the point where that was possible.

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 -

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

Haacked wrote re: Opinionated Input Builders for ASP.Net MVC using partials – Part 1
on 06-11-2009 12:41 AM

Are you using the data annotations? Shouldn't the LabelAttribute be DisplayAttribute?

DotNetKicks.com wrote Opinionated Input Builders for ASP.Net MVC using partials
on 06-11-2009 1:43 AM

You've been kicked (a good thing) - Trackback from DotNetKicks.com

很少看到 wrote re: Opinionated Input Builders for ASP.Net MVC using partials – Part 1
on 06-11-2009 4:43 AM

很少看到这样的文章

把视图作为模型 进行高级抽象 是个好想法

不过就像他们说的 还有待探讨

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

Asp.Net MVC HTML Helper Alternatives « Corey Coogan wrote Asp.Net MVC HTML Helper Alternatives &laquo; Corey Coogan
on 06-11-2009 9:44 AM

Pingback from  Asp.Net MVC HTML Helper Alternatives « Corey Coogan

9eFish wrote Opinionated Input Builders for ASP.Net MVC using partials
on 06-11-2009 10:27 PM

9efish.感谢你的文章 - Trackback from 9eFish

5x1llz wrote re: Opinionated Input Builders for ASP.Net MVC using partials – Part 1
on 06-11-2009 10:56 PM

this sort of builds on Palermos example in MVC in action I like.. might leverage TextBoxFor instead though... and are there options to the attributes?

BorisCallens wrote re: Opinionated Input Builders for ASP.Net MVC using partials – Part 1
on 06-12-2009 4:18 AM

I like the idea.

It would make sence to me to have constraints in my model. Constraints are not view dependant, they should always be there anyway.

As long as you put the desired partial annotation in your DTO, I'm perfectly fine with it.

I feel this has dftly potential.

progg.ru wrote Opinionated Input Builders for ASP.Net MVC using partials – Part 1 - Eric Hexter -
on 06-12-2009 12:07 PM

Thank you for submitting this cool story - Trackback from progg.ru

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

ASP.NET MVC Archived Blog Posts, Page 1 wrote ASP.NET MVC Archived Blog Posts, Page 1
on 06-14-2009 10:57 PM

Pingback from  ASP.NET MVC Archived Blog Posts, Page 1

WebDevVote.com wrote re: Opinionated Input Builders for ASP.Net MVC using partials – Part 1
on 06-15-2009 6:24 AM

Track back from WebDevVote.com

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

Saurabh wrote re: Opinionated Input Builders for ASP.Net MVC using partials – Part 1
on 06-24-2009 10:02 AM

Hi

I am using this

<%= Html.Input("Title",x=>x.Title) %>

Html.Input doesn't resolve.

any idea? which assembly i am missing

Kind Regards

Saurabh

erichexter wrote re: Opinionated Input Builders for ASP.Net MVC using partials – Part 1
on 06-24-2009 8:20 PM

@Saurabh  Try the following ... Add a reference to the InputBuilder project/assembly from your project and than add the <%@ Import Namespace="InputBuilder" %> tag in your view or in the web.config in your Views directory.

Scott Hanselman's Computer Zen wrote The Weekly Source Code 43 - ASP.NET MVC and T4 and NerdDinner
on 06-25-2009 6:42 PM
ASPInsiders wrote The Weekly Source Code 43 - ASP.NET MVC and T4 and NerdDinner
on 06-25-2009 7:04 PM

I really advocate folks reading as much source as they can because you become a better writer by reading

Twitted by stormid wrote Twitted by stormid
on 06-27-2009 5:51 PM

Pingback from  Twitted by stormid

VS2010学习 wrote Using FluentValidation with ASP.NET MVC Opinionated Input Builders
on 06-30-2009 3:34 AM

I’ve been following Eric Hexter’s series on using opinionated input builders with ASP.NET

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:16 AM

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

The Weekly Source Code 43 - ASP.NET MVC and T4 and NerdDinner | CHARGED's Digital Lifestyle at Work or Play wrote The Weekly Source Code 43 - ASP.NET MVC and T4 and NerdDinner | CHARGED's Digital Lifestyle at Work or Play
on 07-05-2009 10:07 AM

Pingback from  The Weekly Source Code 43 - ASP.NET MVC and T4 and NerdDinner | CHARGED's Digital Lifestyle at Work or Play

RassaR wrote re: Opinionated Input Builders for ASP.Net MVC using partials – Part 1
on 07-07-2009 10:47 AM

Hi

I got model with list of 'subject' fields

public IList<string> subject { get; set; }

I'm trying to create equivalent of

<input type="text" name="subject[0]">

<input type="text" name="subject[1]"> etc

How do i do that with input builder ?

<%= Html.Input(c => c.subject[0]) %> does not work

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Line 127:        public static object ValueFromModelProperty(PropertyInfo propertyInfo,object model)

Line 128:        {

Line *129*:            var value = propertyInfo.GetValue(model, new object[0]);

Source File: C:\work\InputBuilder\DefaultConventions.cs    Line: 129

erichexter wrote re: Opinionated Input Builders for ASP.Net MVC using partials – Part 1
on 07-07-2009 1:07 PM

@RassaR the index logic can handle the arrays but the value logic needs to be changed to pull that value out properly.  I will need to setup a spike to run through that example.

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 -

VS2010学习 wrote The Weekly Source Code 43 - ASP.NET MVC and T4 and NerdDinner
on 08-14-2009 6:10 AM

UPDATE: David's put the T4 template with some nice updates on CodePlex - It's the last download

Jeffrey Palermo (.com) wrote MvcContrib 1.0.0.987 released
on 09-23-2009 3:02 PM

Eric Hexter and I just released a new drop of MvcContrib this morning.  In short, head over to the project page and check out the new download.  It has many patches applied since March, 2009 as well as Eric Hexter’s “Opinionated Input Builders

Jimmy Bogard wrote MVC Web Testing Strategies – verifying content
on 10-27-2009 9:51 PM

Some of the questions during the C4MVC presentation concerned how I liked to locate data displayed in

Jeremy D. Miller -- The Shade Tree Developer wrote Shrink your Views with FubuMVC Html Conventions
on 01-29-2010 12:03 PM

Yesterday I was able to “push” into Github the work I’ve been doing for convention

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