How to default value after databound

3 posts, 1 answers
  1. Greg
    Greg avatar
    28 posts
    Member since:
    Dec 2014

    Posted 20 Feb 2015 Link to this post

    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. Answer
    Georgi Krustev
    Admin
    Georgi Krustev avatar
    3718 posts

    Posted 24 Feb 2015 Link to this post

    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.

     
  3. Greg
    Greg avatar
    28 posts
    Member since:
    Dec 2014

    Posted 24 Feb 2015 in reply to Georgi Krustev Link to this post

    Thanks  using it as: .Value(ViewData["defaultLocationValue"].ToString())

    Did the trick.
Back to Top