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.
Unfortunately, activation email could not send to your email. Please try again.

Setting selectedItemIndex programmatically

Thread ID:

Created:

Updated:

Platform:

Replies:

117488 Oct 29,2014 01:16 PM Nov 3,2014 07:06 AM JavaScript 3
loading
Tags: ejDropDownList
Randy Craven
Asked On October 29, 2014 01:16 PM

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

Kasithangam C [Syncfusion]
Replied On October 30, 2014 10:58 AM

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


Randy Craven
Replied On October 30, 2014 03:57 PM

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

Saranya Sivakumar [Syncfusion]
Replied On November 3, 2014 07:06 AM

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

CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.

;