I've done it like this. Write your own ExtensionMethod for CheckBoxFor.
The trick is the static values for "value" = "true" in the checkbox and the "value" "false" in the hidden field.
As mentioned before, a checkbox with a value false will not be sent back. In this case the value of the hidden field will be taken. When the checkbox is checked by the user, the new "true"-value will override the "false" from the hidden field.
public static MvcHtmlString CheckboxForMetro<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, int offset = 3)
{
TagBuilder tblabel = new TagBuilder("label");
tblabel.AddCssClass("checkbox offset" + offset.ToString());
TagBuilder tbinput = new TagBuilder("input");
tbinput.Attributes.Add("type", "checkbox");
tbinput.Attributes.Add("id", GetPropertyNameFromLambdaExpression(html, expression));
tbinput.Attributes.Add("name", GetPropertyNameFromLambdaExpression(html, expression));
tbinput.Attributes.Add("value", "true");
tbinput.MergeAttributes(GetPropertyValidationAttributes(html, expression, null));
if (GetPropertyValueFromLambdaExpression(html, expression) == "True") tbinput.Attributes.Add("checked", "checked");
TagBuilder tbhidden = new TagBuilder("input");
tbhidden.Attributes.Add("type", "hidden");
tbhidden.Attributes.Add("value", "false");
tbhidden.Attributes.Add("name", GetPropertyNameFromLambdaExpression(html, expression));
TagBuilder tbspan = new TagBuilder("span");
//tbspan.AddCssClass("span" + spanLabel.ToString());
tbspan.InnerHtml = GetPropertyDisplayNameFromLambdaExpression(html, expression);
tblabel.InnerHtml = tbinput.ToString() + tbspan.ToString() + tbhidden.ToString();
return new MvcHtmlString(tblabel.ToString());
}
It's an ExtensionMethod for the Metro UI CSS at http://metroui.org.ua
This is the code for getting the value, displayname, propertyname and validationAttributes
private static string GetPropertyDisplayNameFromLambdaExpression<TModel, TValue>(HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression)
{
ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, html.ViewData);
string htmlFieldName = ExpressionHelper.GetExpressionText(expression);
return metadata.DisplayName ?? metadata.PropertyName ?? htmlFieldName.Split('.').Last() ?? "Geen tekst";
}
private static string GetPropertyValueFromLambdaExpression<TModel, TValue>(HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression)
{
string value = string.Empty;
TModel model = html.ViewData.Model;
if (model != null)
{
var expr = expression.Compile().Invoke(model);
if (expr != null)
{
value = expr.ToString();
}
}
return value;
}
private static string GetPropertyNameFromLambdaExpression<TModel, TValue>(HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression)
{
ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, html.ViewData);
return metadata.PropertyName;
}
private static IDictionary<string, object> GetPropertyValidationAttributes<TModel, TValue>(HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, IDictionary<string, object> htmlAttributes)
{
ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, html.ViewData);
IDictionary<string, object> validationAttributes = html.GetUnobtrusiveValidationAttributes(ExpressionHelper.GetExpressionText(expression), metadata);
if (htmlAttributes == null)
{
htmlAttributes = validationAttributes;
}
else
{
htmlAttributes = htmlAttributes.Concat(validationAttributes).ToDictionary(k => k.Key, v => v.Value);
}
return htmlAttributes;
}
I hope this will help someone else.
@Html.CheckboxFor(model => model.W2_Sent)make a difference?