ASP.NET MVC 2 - Html.EditorFor a nullable type?
Thanks to Bryan for adding a bounty to try to get a positive solution, but I'm going to have to answer and say that I have found that the answer is definitely NO - you cannot have a nullable template auto-discovered from its type. You must use a template name.
This is the relevant quote from Brad Wilson's blog at http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-3-default-templates.html. He is an authoritative source on MVC so I have to believe him when he says:
When searching for the type name, the simple name is used (i.e., Type.Name) without namespace. Also, if the type is Nullable, we search for T (so you’ll get the Boolean template whether you’re using “bool” or “Nullable”)
He also goes on to say
This means if you’re writing templates for value types, you will need to account for whether the value is nullable or not. You can use the IsNullableValueType property of ModelMetadata to determine if the value is nullable. We’ll see an example of this below with the built-in Boolean template.
So YES there is an answer to this question, but unfortunately the answer is NO.
To use a nullable template you must explictly use the template name:
<%: Html.EditorFor(model => model.SomeNullableDecimal, "NullableDecimalTemplate" )%>
Or you can use one template that handle both the nullable and the non nullable type:
<% if (ViewData.ModelMetadata.IsNullableValueType) { %>
<%= Html.DropDownList("", TriStateValues, new { @class = "list-box tri-state" })%>
<% } else { %>
<%= Html.CheckBox("", Value ?? false, new { @class = "check-box" })%>
<% } %>
The template would have to be named "Nullable`1". Since this would match any Nullable struct, you could do a switch on the model type and render the appropriate partial template based on the type from within the "Nullable`1.ascx"
In order to create a template for a nullable type, you name your template as the base value type and then create your editor template with a nullable model.
For example, I want to do a template for int?. I created an editor template named "int32.cshtml" and I'm using int? as the model.