Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kendo Mvc DropdownList, How to set Initial Value

i am using Kendo DropdownList with ajax binding.

With these codes i cannot set inital value that coming from model.It just fill the list with items and selects the first item.

 @(Html.Kendo().DropDownList()
 .Name("NATIONALITY") 
 .DataTextField("DESCRIPTION").HtmlAttributes(new { style = "width:220px" }) 
 .DataValueField("REFERENCEID")
 .Value(Model.NATIONALITY)
         //Placeholder("SELECT")
  .DataSource(source =>
                        {
  source.Read(read =>
             {
        read.Action("GetDefinitionDetails", "Definition", new { definitionCode = "NATIONALITY", addEmptyRow = false });
                            }).ServerFiltering(true);
                            }).ServerFiltering(true);
                        }))

But When i change Html.Kendo().DropDownList() to Html.Kendo().Combobox() it also fills the list and set the initial value as expected(which is the value that passing by model.)

like image 880
balron Avatar asked Dec 16 '22 03:12

balron


2 Answers

On the Kendo DropDownList you can specify the SelectedIndex as follows:

@(Html.Kendo().DropDownList().SelectedIndex(myIndex)
 .//other builder stuff

With the DropDownList you can only specify the index of the item that is to be selected. You cannot chose this item by its text.

Using the client-side API you can set set selected value based on the text, by using the value method. Just add an event on the data bound that calls a JavaScript function. This function can then select the desired item based on its text.

like image 131
Andrei V Avatar answered Dec 28 '22 13:12

Andrei V


With the latest(maybe works on earlier versions) build of KendoUI , initial Text value can be set on dropdowns:

   ....
   .Value(Model.DOCTORCODE.ToString()) //DOCTORCODE is  guid type
   .Text(Model.DOCTOR)  //DOCTOR is string type.This is optional.It should work without .Text property
like image 30
balron Avatar answered Dec 28 '22 13:12

balron