We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Setting selectedItemIndex programmatically

Hello,

My MVC project uses a (javascript) dropDownList with Json data source.  When loading the view I would like to assign the selectedItemIndex  if a previous selection was made, or have the list be empty (null selected value) if no selection has been made.  Something like this:

@{
    int index = -1;
    if(Model.previouslySelectedIndex > -1){
        index = Model.previouslySelectedIndex;
    }
}

<script>
    $('#ddl').ejdropdownlist({
        dataSource: myJson,
        selectedItemIndex: Number(@index)
    });
</script>

It appears that using -1 for the selectedItemIndex does not work;  the selectedItemIndex appears to be set to 0 instead.  Using null throws an error.

I also tried using the MVC wrapper and it appears that you can successfully assign the value of -1.   However, for other reasons I need to use the plain javascript controls.

Can anyone suggest the best way to accomplish this?

thank you,
Randy

3 Replies

KC Kasithangam C Syncfusion Team October 30, 2014 02:58 PM UTC

Hi Randy,

Sorry for the inconvenience caused. We are able to reproduce the specified issue. We have confirmed this as bug (“while setting the “selectedItemIndex” property value as -1 in dropdownlist, the dropdown value must be an empty and while setting the “selectedItemIndex” property as null, it throws an error.”) and an issue report has been logged on this. If you are in need of a solution sooner we can also provide a custom patch for this within 15days.

As a workaround solution, Instead of specify the “selectedItemIndex” property value in the control creation, we have set the index variable value as -1 and checked the condition with that value. In this, we have to manually empty the dropdown value when the index variable value -1 otherwise the index value is set to the “selectedItemIndex” property as shown below,

[Index.cshtml]

   <script type="text/javascript">

        var target;

        $(function () {

           var index = -1;

           var target = $("#bikeList").data("ejDropDownList");

            if (index > -1)

                target.option("selectedItemIndex",index)

            else

                target.option("value", "");

         });

Also we have prepared the sample based on this and it is available under the following location,

Sample location: Sample

Kindly let us know if you have any queries.

Regards,

Kasithangam



RC Randy Craven October 30, 2014 07:57 PM UTC

Hello Kasithangam,

Thank you for this reply.  The workaround you posted is sufficient for me and a custom patch is not necessary.

I'm glad it was a real bug and not an imagined one.

Thanks again,
Randy


SS Saranya Sivakumar Syncfusion Team November 3, 2014 12:06 PM UTC

Hi Randy,
Thanks for your update.
We are glad to hear that the issue has been resolved in your side. 
Please let us know if you have further concern.
Regards,
Saranya.S

Loader.
Live Chat Icon For mobile
Up arrow icon