I've been wrestling with the same issue today for a checkbox that binds to a nullable bool, and since I can't change my model (not my code) I had to come up with a better way of handling this. It's a bit brute force, but it should work for 99% of cases I might encounter. You'd obviously have to do some manual population of valid attributes for each input type, but I think I've gotten all of them for checkbox.
In my Boolean.cshtml editor template:
@model bool?
@{
var attribs = new Dictionary<string, object>();
var validAttribs = new string[] {"style", "class", "checked", "@class",
"classname","id", "required", "value", "disabled", "readonly",
"accesskey", "lang", "tabindex", "title", "onblur", "onfocus",
"onclick", "onchange", "ondblclick", "onmousedown", "onmousemove",
"onmouseout", "onmouseover", "onmouseup", "onselect"};
foreach (var item in ViewData)
{
if (item.Key.ToLower().IndexOf("data_") == 0 || item.Key.ToLower().IndexOf("aria_") == 0)
{
attribs.Add(item.Key.Replace('_', '-'), item.Value);
}
else
{
if (validAttribs.Contains(item.Key.ToLower()))
{
attribs.Add(item.Key, item.Value);
}
}
}
}
@Html.CheckBox("", Model.GetValueOrDefault(), attribs)