Search code examples
kendo-gridkendo-asp.net-mvc

MVC Kendo Combobox set value from the DataSource


I have this code, how can I set the Value property to the first element within the DataSource?

  @(Html.Kendo().ComboBox()
                          .Name("cboShip")
                          .Placeholder("Select Ship...")
                          .Filter(FilterType.Contains)
                          .DataTextField("ShipName")
                          .DataValueField("ShipId")
                          .DataSource(s =>
                          {
                            s.Read(read => { read.Action("GetShips", "Filter"); });
                          })
                          .Events(events => events.Change("cboShipMultiChange"))
                         .Value('' /*????? I want to get the first element from the DataSource and use it in here*/)
   )

Solution

  • from my comments:

    you should be able to just apply .SelectedIndex (0) rather than a value. in the declaration

    @(Html.Kendo().ComboBox()
                  .SelectedIndex(0) //or whatever index you want to apply. 
                  ....... removed for brevity
    )