Processing null types with an editor template

I designed an editor template that takes boolean types and creates a drop-down list to change the default values true and false in Si and No Now that I have deployed the application, I did not understand that the boolean and Nullable<boolean> tags are taken as the same as for the name of my editor template (boolean.cshtml). Now I don’t want this, I want to change the editorfor behavior only when the model data type was Nullable<boolean> not when it was boolean. How can I handle this?

  @model Nullable<bool> @{ var listItems = new[] { new SelectListItem { Value = "true", Text = "Si" }, new SelectListItem { Value = "false", Text = "No" } }; } @Html.DropDownListFor( model => model.Value, listItems) 
+4
c # asp.net-mvc asp.net-mvc-3
source share
2 answers

You cannot stop them from using the same editor template. But you can handle it in an editor template. You have:

 ViewData.ModelMetadata.IsNullableValueType 

If you read this article, you can see what the default template does with this to switch between two different visualizations. Just steal from him:

http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-3-default-templates.html

+14
source share

My Views / Shared / EditorTemplates / Boolean.cshtml works both ways, handling empty and regular checkboxes.

 @model bool? @{ if (ViewData.ModelMetadata.IsNullableValueType) { <text><div class="RB"></text> Dictionary<string, object> yesAttrs = new Dictionary<string, object>(); Dictionary<string, object> noAttrs = new Dictionary<string, object>(); Dictionary<string, object> nullAttrs = new Dictionary<string, object>(); yesAttrs.Add("id", ViewData.TemplateInfo.GetFullHtmlFieldId("") + "Yes"); noAttrs.Add("id", ViewData.TemplateInfo.GetFullHtmlFieldId("") + "No"); nullAttrs.Add("id", ViewData.TemplateInfo.GetFullHtmlFieldId("") + "NA"); if (Model.HasValue && Model.Value) { yesAttrs.Add("checked", "checked"); } else if (Model.HasValue && !Model.Value) { noAttrs.Add("checked", "checked"); } else { nullAttrs.Add("checked", "checked"); } @Html.RadioButtonFor(x => x, "true", yesAttrs) <label for="@(ViewData.TemplateInfo.GetFullHtmlFieldId(""))Yes">Yes</label> @Html.RadioButtonFor(x => x, "false", noAttrs) <label for="@(ViewData.TemplateInfo.GetFullHtmlFieldId(""))No">No</label> @Html.RadioButtonFor(x => x, "", nullAttrs) <label for="@(ViewData.TemplateInfo.GetFullHtmlFieldId(""))NA" class="nostrong" title="Unknown or To Be Determined">tbd</label> @:</div> } else { ModelState state = ViewData.ModelState[ViewData.ModelMetadata.PropertyName]; bool value = Model ?? false; if (state != null && state.Errors.Count > 0) { <div class="input-validation-error" style="float: left"></div> } else { @Html.CheckBox("", value) } } } 

Drop-down lists for only 2-3 selected items are not relevant to users; they require two clicks, while the switches require only one click (if you do not have real estate).

When your customers / users haven’t made a decision, don’t want to make a decision, don’t think that you know that the answer is your business, or you need to reset true/1/on/positive or false/0/off/negative since none of them is not accurate, the third parameter for Nullable<boolean> may be "to be determined" (tbd), not specified, unknown or not applicable (n / a).

+3
source share

All Articles