Kendo Dropdownlist for nullable bool doesn't set to null

@model bool?
@(Html.Kendo().DropDownListFor(m=>m).BindTo(new List<SelectListItem>() {
                                          new SelectListItem() {
                                          Text = "--Select--",
                                          Value = null
                                      },
                                      new SelectListItem() {
                                          Text = "Yes",
                                          Value = "true"
                                      },
                                      new SelectListItem() {
                                          Text = "No",
                                          Value = "false",
                                      }

        }).DataTextField("Text").DataValueField("Value").HtmlAttributes(new { data_value_primitive = "true" }))

      

Everything works with one problem: when I set the value to both true and false, I cannot set the value to null. So if the field value was "true" and I set it to "null" or ("--Select--") and click Submit, the value will be set to false instead. This is how a field is created in my model

[DisplayName("Autostart Load")]
public bool? AutoStartLoad { get; set; }

      

Why is this happening?

+3


source to share


1 answer


Try the property OptionLabel

, it works for me

@Html.Kendo().DropDownListFor(model => model.SomeProperty).OptionLabel("Choose value...").BindTo(new List<SelectListItem>()
            {
                new SelectListItem() { Text = "Yes", Value = "True" },
                new SelectListItem() { Text = "No", Value = "False" },
            });

public bool? SomeProperty { get; set; }

      



Also set Value

out true

and false

, starting with a capital letter, because it bool.ToString()

returns the value as "True"

and"False"

+2


source







All Articles