Search code examples
asp.net-mvcrazorkendo-uikendo-dropdown

Kendo DropDown default value does not seem to work


I am using Kendo Drop down with MVC and binding it with a model of Name,value pair. Pretty standard. Now, I want it to start with a blank option IF the model does not currently have any value for that field which is "Vendor" in this case.

Which mean New Vendor --> Empty Dropdown default value. Existing vendor --> Current Value

I have following code to fulfill that :

 @(Html.Kendo().DropDownList()
                  .Name("Vendor")
                  .DataTextField("Name")
                  .DataValueField("VendorId")
                  .Text(Model.Vendor == null ? "" : (Model.Vendor.Name))
                  .Value(Model.Vendor == null ? "" : Model.Vendor.VendorId.ToString())
                  .DataSource(source => source.Read(read => read.Action("GetAllVendors", "Vendors")))
                  )
        </td>

The model that I am sending over has Vendor == Null but still the default value is not Empty but instead it has one of the values that "GetAllVendors returned"

What am I doing wrong?


Solution

  • You can add .OptionLabel("--Please select vendor--") to the Kendo drop-down list builder expression to show a user-friendly message for selecting a vendor from your remote list.