Helpers for the MS MVC Framework

Go to the bottom of this post for the download.

There's a discussion going on at the MvcContrib site about including UI Helpers in the MvcContrib project. The discussion seems to be centralising around the issue of whether helpers should be implemented through extension methods (the approach the MVCToolkit takes), regular static methods or instance methods.

My preference is for instance methods on dedicated helper classes. This approach is the most flexible as it allows you to:

I decided to put together a sample application that uses an IoC container (Windsor) for creating helpers. The sample project includes:

Some sample code:

<% Form.For("person", new Hash(action => "Save"), form => { %>
	Surname: <%= form.TextField("Surname", new Hash(style => "width: 200px")) %>
	<br />
	Forename: <%= form.TextField("Forename", new Hash(style => "width: 200px"))%>
	<br /><br />
	<%= form.HiddenField("id") %>
	<%= form.Submit() %>
<% }); %>

Which generates...

<form method="post" action="/Home/Save">
	Surname: <input type="text" id="person_Surname" name="person.Surname" value="Smith" style="width: 200px" />
	<br />
	Forename: <input type="text" id="person_Forename" name="person.Forename" value="Jane" style="width: 200px" />
	<br /><br />
	<input type="hidden" id="person_id" name="person.id" value="2" />
	<input type="submit" value="Submit" />
</form>

Note: To run the sample you will need the .NET 3.5 Extensions CTP installed.

Disclaimers:

The download can be found here.