Simple question really.
I have MVC view that displays a Nullable Bool, e,g,
Html.CheckBoxFor(model=>model.NullableBoolHere, Model.NullableBoolHere,
and I want to create a new html helper, that will accept this type, and then convert
Null || False => False
True => True
so I have the following
public static MvcHtmlString CheckBoxFor<TModel>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, bool?>> expression, object htmlAttributes, bool disabled)
{
IDictionary<string, object> values = new RouteValueDictionary(htmlAttributes);
if (disabled)
values.Add("disabled", "true");
Expression<Func<TModel, bool>> boolExpression = CONVERT_TO_BOOL_HERE(expression);
return htmlHelper.CheckBoxFor(expression, values);
}
Any help appreciated, I understand I will have to use recursion to copy the expression, but just not sure how to go about navigating the expression itself, find the bool?, convert to bool.
So, in the end, the only way I could find to do it was to resolve the bool? into a bool myself, then return a ‘normal’ checkbox by passing in the correct name etc.
This does work a treat though, so all good. If you do know a better way of getting the correct ParameterName, it would be great to hear.