4
votes

How to set default item in HTML.Kendo().Dropdownlist().HTMLAttribute() ?

Although i have set it by code.But somehow its taking one default item from the list and showing it as first place.

enter image description here

May be its getting it somewhere from the code.But i am not able to finding out.And that's why i want to set the default item("--Select--") from somewhere else. And i am thinking about setting it from .HTMLAttributes().

Is someone knows its a correct way to do it or not ? If yes then how can we do that? Else no then is there any other way to fix it?

I am using below code to bind the Kendo Dropdownlist:

 @(Html.Kendo().DropDownList()
  .Name("SelectedMediaType")  
  .DataTextField("Text")
  .DataValueField("Text")    
  .Value(Model)                                    
  .BindTo(ViewBag.MediaTypes)

 )

In which i am binding Dropdownlist with ViewBag.MediaTypes .

So is there any way to set default value after binding with viewbag?

2

2 Answers

3
votes

I'm very new to KendoUI, but I believe you can just set the default by using the .value property: http://docs.kendoui.com/api/web/dropdownlist#configuration-value

$("#dropdownlist").kendoDropDownList({
     dataSource: ["Car", "Bike", "T.V", "--Select One--"],
     value: "--Select One--"
});
3
votes

The only way I was able to get it to work was by handling the DataBound event, and setting the default value there.

In this example I'm binding to an action on the server, so perhaps you only need the .Events line and the JavaScript at the end:

            @(Html.Kendo().DropDownListFor(model => model.SalesPersonID)
            .DataTextField("FullName")
            .DataValueField("ID")
            .Events(ev => ev.DataBound("SalesPersonID_DataBound"))
            .DataSource(source =>
            {
                source.Read(read =>
                {
                    read.Action("GetSalesPersonList", "Proposal");                       
                });
            })
            )

            <script type="text/javascript">
                //Sets the default value for the DropDownList.
                function SalesPersonID_DataBound() {
                    this.value("@Model.SalesPersonID");
                }
            </script>

I suspect the problem is this: if the value is set before or during binding, it is lost. You need to set it right after binding completes, and the DataBound event is the best way to do that.