Dependent Dropdownlist Doesn't Update

Hi everyone,

i’m populating dropdownlist based on anther dropdownlist here is my view code:

<?php

        echo &#036;form-&gt;dropDownList(&#036;model,'menu_category_id', 


        CHtml::listData(MenuCategory::model()-&gt;findAll(), 'id', 'name'),





        array(


            'prompt'=&gt;'Select Menu Category',


            'ajax' =&gt; array(


            'type'=&gt;'POST', 


            'url'=&gt;CController::createUrl('GetMenuType'),


            'update'=&gt;'#menu_type_id', 


            'data'=&gt;array('menu_category_id'=&gt;'js:this.value'),


        ))); 


        


        echo &#036;form-&gt;error(&#036;model,'menu_category_id');


        echo '&lt;/div&gt;';


        echo '&lt;div class=&quot;row&quot;&gt;';


        echo &#036;form-&gt;labelEx(&#036;model,'menu_type_id');


        // replace the live below 


        echo &#036;form-&gt;dropDownList(&#036;model,'menu_type_id', array(), array('prompt'=&gt;'Select Menu Type')); 


        ?&gt; 


       &lt;?php echo &#036;form-&gt;error(&#036;model,'menu_type_id'); ?&gt; 

above code works perfectly fine if i use this

echo CHtml::dropDownList(‘menu_type_id’,’’, array(), array(‘prompt’=>‘Select Menu Type’));

but when i add that array into $form->dropdownlist(… it doesn’t work

any help below is my controller code.

public function actionGetMenuType()

    {


        &#036;models = MenuType::model()-&gt;findAll('menu_category_id=:menu_category_id',


                array(':menu_category_id'=&gt;&#036;_POST['menu_category_id']));


        


        &#036;list = CHtml::listData(&#036;models, 'id', 'name');


        


        echo &quot;&lt;option value=''&gt;Select Menu Type&lt;/option&gt;&quot;;


        foreach(&#036;list as &#036;id=&gt;&#036;name){    


            echo CHtml::tag('option',


                    array('value'=&gt;&#036;id), CHtml::encode(&#036;name), true);


        }


        


    }

Hi MJ0078, welcome to the forum.

As you have figured out, CHtml::dropDownList and CActiveForm::dropDownList behave differently.

The latter gives the input an id using CHtml::activeId method. It uses the model name and the attribute name to create the id string.

So you have to specify the ajax updated input as:




     'update'=>'#' . CHtml::activeId($model, 'menu_type_id'),



http://www.yiiframework.com/doc/api/1.1/CHtml#activeId-detail