This is a migrated thread and some comments may be shown as answers.

How to default value after databound

2 Answers 1491 Views
DropDownList
This is a migrated thread and some comments may be shown as answers.
Greg
Top achievements
Rank 1
Greg asked on 20 Feb 2015, 03:40 PM
I want to have the dropdown default select a value.  I have the value is in a ViewData.  How can I access this from javascript?

@(Html.Kendo().DropDownList()
                       .Name("locationCode")
                       .HtmlAttributes(new { style = "width:200px" })
                       .DataValueField("Code")
                       .DataTextField("Name")
                       .DataSource(source =>
                                    {
                                        source.Read(read =>
                                        {
                                            read.Action("GetUserGlobalDimensions", "RequestPONumber");
                                        });
                                    }

                                  )
                        .Events(e => { e.DataBound("onLocationDataBound"); })

In js file:
function onLocationDataBound() {
    this.value('ViewData["defaultLocationValue"]');
}

Thanks in advance.



2 Answers, 1 is accepted

Sort by
0
Accepted
Georgi Krustev
Telerik team
answered on 24 Feb 2015, 09:38 AM
Hello Greg,

I would suggest you use the Value method that will set the widget's option value. This will select the corresponding data item on load:
@(Html.Kendo().DropDownList()
                       .Name("locationCode")
                       .HtmlAttributes(new { style = "width:200px" })
                       .DataValueField("Code")
                       .DataTextField("Name")
                       .DataSource(source =>
                                    {
                                        source.Read(read =>
                                        {
                                            read.Action("GetUserGlobalDimensions", "RequestPONumber");
                                        });
                                    }
 
                                  )
                        .Events(e => { e.DataBound("onLocationDataBound"); })
                        .Value(ViewData["defaultLocationValue"])
)

Let me know if I am missing something.

Regards,
Georgi Krustev
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Greg
Top achievements
Rank 1
answered on 24 Feb 2015, 02:30 PM
Thanks  using it as: .Value(ViewData["defaultLocationValue"].ToString())

Did the trick.
Tags
DropDownList
Asked by
Greg
Top achievements
Rank 1
Answers by
Georgi Krustev
Telerik team
Greg
Top achievements
Rank 1
Share this question
or