Skip to content

Instantly share code, notes, and snippets.

@anthonyvscode
Created December 18, 2013 07:06
Show Gist options
  • Save anthonyvscode/8018447 to your computer and use it in GitHub Desktop.
Save anthonyvscode/8018447 to your computer and use it in GitHub Desktop.
MVC EditorTemplate for Nullable Booleans
@model bool?
@if (ViewData.ModelMetadata.IsNullableValueType) {
var items = new[]
{
new SelectListItem { Value = "", Text = "N/A" },
new SelectListItem { Value = "true", Text = "Yes" },
new SelectListItem { Value = "false", Text = "No" }
};
@Html.DropDownList("", new SelectList(items, "Value", "Text", Model.HasValue ? (Model.Value ? "true" : "false") : ""))
} else {
@Html.CheckBox(string.Empty, Model.GetValueOrDefault(false))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment