how to get selected value for Kendo DropDownList

若如初见. 提交于 2019-12-04 05:14:31

Maybe you should be using the DropDownListFor construct of the Kendo DropDownList like so in your view:

@(Html.Kendo().DropDownListFor(m => m.EncSelected)
                    .Name("EncounterTypes")
                    .DataTextField("Description")
                    .DataValueField("ID")
                    .BindTo(Model.ENCTypes)
                    .SelectedIndex(Model.EncSelected)
                )

This way, when you submit, it will be availble on the POST request and you won't need to put an hidden field anywhere.

BUT should you need to use the hidden field for some reason, put it there, subscribe the the select event of the dropdown list and put using JQuery (for instance) put the selected item on the hidden field.

It's your choice :)

For anyone who found this wondering how to get the selected value in JavaScript, this is the correct answer:

$("#EncounterTypes").data("kendoDropDownList").value();

From the documentation: http://docs.telerik.com/kendo-ui/api/javascript/ui/dropdownlist#methods-value

when select a value from a dropdown list, and in the selec event , we can get the selected value as following ,

@(Html.Kendo().DropDownList()
              .Name("booksDropDown")
              .HtmlAttributes(new { style = "width:37%" })
              .DataTextField("BookName")
              .DataValueField("BookId")
              .Events(x => x.Select("onSelectBookValue"))
              .DataSource(datasource => datasource.Read(action => action.Action("ReadBookDropDow", "PlanningBook").Type(HttpVerbs.Get)))
              .OptionLabel("Select"))

javascript function like following ,

   function onSelectBookValue(e) {    

                    var dataItem = this.dataItem(e.item.index());
                    var bookId = dataItem.BookId;
                 //other user code
    }

I believe this will help someone

Thanks

Hello I was just going through this problem,kept on searching for 2 hours and came up with a solution of my own.

So here is the line to fetch any data bidden to the kendo drop down.

$("#customers").data("kendoDropDownList").dataSource._data[$("#customers").data("kendoDropDownList").selectedIndex].colour;

Just change the id customers to the id you have given tot he kendo drop down.

If you want to read also out the text of the dropdown, you can get or set the value by using the following kendo function:

$('#EncounterTypes').data("kendoDropDownList").text();

REFERENCE TO THE DOCUMENTATION

Using this .val() as @Vivek Parekh mentions will not work - there is no function .val() in the kendo framework.

If you want you could use jQuery and get the value back: $('#EncounterTypes').val()

Updated DEMO

$("#EncounterTypes").kendoDropDownList().val();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!