DropDownListFor - clear items and repopulate with JS

2 posts, 0 answers
  1. Shahar
    Shahar avatar
    12 posts
    Member since:
    Jul 2010

    Posted 24 Jul 2013 Link to this post

    Hi,
    I have a DropDownList that is initially binded on the server side with BindTo method (I am using DropDownListFor).
    On the client (JS code) I need to clear its items and then repoppulate the control with other items.
    What is the best practice to accomplish this?

    Thanks,
    Lauri
  2. Petur Subev
    Admin
    Petur Subev avatar
    1882 posts

    Posted 26 Jul 2013 Link to this post

    Hello Shachar,

    Basically to control what items are used by the DropDownList you need to use the dataSource objects and its methods.

    In your case you will need to get reference to the client object and use the dataSource.data() method. 

    e.g.

    var ds = $('#ddlName').data().kendoDropDownList.dataSource;
    ds.data([{PersonID:1,FirstName:"Shachar"},{PersonID:2,FirstName:"Petur"}])
    // make sure that the objects that you use to populate the dataSource have the same fields as the ones that you specified in the DataTextField and DataValueField of your DropDownList

    I hope this helps.

    Kind Regards,
    Petur Subev
    Telerik
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
  3. UI for ASP.NET MVC is VS 2017 Ready
Back to Top