Unable to preselect items in a listbox

I have a ASP.NET MVC razor listbox that is generated using the following code :-


@Html.EJS().ListBox("selectTABToNavigateTo").Scope("combined-list").Height("500px").Change("onSelectTabNavigation").Fields(new Syncfusion.EJ2.DropDowns.ListBoxFieldSettings { Text = "Label", Value = "Id" }).DataSource((IEnumerable<CustomFormElement>)ViewBag.selectedTabbedFieldList).SelectionSettings(new Syncfusion.EJ2.DropDowns.ListBoxSelectionSettings { Mode = Syncfusion.EJ2.DropDowns.SelectionMode.Single}).Render()

This all renders fine and is usable, however, I want to be able to preselect an item, where this will be done in JavaScript.

    var tabSelect = document.getElementById("selectTABToNavigateTo").ej2_instances[0];
    tabSelect.selectItems([]);

    if (currentlySelectedTabNavigation !== undefined) {
        var tabSelectListData = document.getElementById("selectTABToNavigateTo").ej2_instances[0].listData;
        var count = 0;
        for (const item of tabSelectListData) {
            if (item.RecordId == currentlySelectedTabNavigation) {
                var theSelectedItems = [item];
                tabSelect.selectItems(theSelectedItems);
            }
        }
    }

I have tried to create a list of Ids and a list of objects nothing seems to work.  What I will say is that if I do select something in the listbox, dismiss the box and then try to look at the listbox again, the item that I have selected is selected and not the item I want pre-selected.

Any help with this will be greatly appreciated.

Thank you

1 Reply 1 reply marked as answer

SP Sangeetha Priya Murugan Syncfusion Team October 19, 2020 01:51 PM UTC

Hi James, 
 
Thank you for contacting Syncfusion support. 
 
We have checked your reported requirement and we suspect that you have rendered the listbox inside the dialog box. And select the items in dialog open event. We suggest you to select the items in listbox created event instead of setting in dialog open event. For your convenience, we have created the sample based on your requirement. Please find the link below. 
 
 
Could you please check the above sample and get back to us, if you need any further assiatance on this. 
 
Regards, 
Sangeetha M 


Marked as answer
Loader.
Up arrow icon