selected option on dropdownlist when actionUpdate

The normal way to make dropdownlist in "_form" is:




<div class="row">

        <?php $pList=project::model()->findAll('complished!=:com',array(':com'=>100)); ?>

        <?php echo $form->labelEx($model,'id_project'); ?>

        <?php echo CHtml::dropDownList('Task[id_project]','id_project', CHtml::listData($pList,'id','project_name'),

                    array('empty'=>'Select Project ')); ?>

        <?php echo $form->error($model,'id_project'); ?>

    </div>



It’s ok when we need to create a new record. But on update action, We need the value was selected.

How to do that?

Thank you

The normal way would be to use


$form->dropDownList($model,'id_project',...)

This way if the attribute $model->id_project would have some value… that value would be selected…

Thank you mdomba… I now understand which one is the ‘normal’ way :D

and if I have a dependent dropdownlist like this:




 <div class="row">

             <?php echo $form->labelEx($model_estado,'uf'); ?>

             <?php

             

             echo $form->dropDownList($model_estado,'uf',$this->getEstados(),                     

                     array(  

                         'options'=>array($model_estado->id=>array('selected'=>'selected')),

                         'empty'=>'Selecione...',

                         'ajax' =>

                         array(

                             'type'=>'POST', //request type                          

                             'url'=>CController::createUrl('getCidadesDoEstado'), //action to call                           

                             'update'=>'#Endereco_cidade', // which HTML element to update                                                                                      

                             

                             )

                         )

                         

                     );

             ?>

             <?php echo $form->error($model_estado,'uf'); ?>

             

	</div>


        <div class="row">

            

                <?php echo $form->labelEx($model_endereco,'cidade'); ?>

        <?php

        echo CHtml::dropDownList(CHtml::activeName($model_endereco, 'cidade'),'',array(),  array(  

                         'empty'=>'Selecione...',

                         'id'=>'Endereco_cidade',            

                         

            

                         

                            )

                );

        ?>

        <?php echo $form->error($model_endereco,'cidade'); ?>    


            

            

	</div>     





 public function actionGetCidadesDoEstado(){

            

           

            

            $idEstado=$_POST['Estado'];

            $listaDeCidades = Cidade::model()->findAllByAttributes(array('estado'=>(int)$idEstado),array('order'=>'nome'));

             echo CHtml::tag(

                       'option', // tagname

                       array('value'=>''), // html params of tag

                       'Selecione...', // value from the item selected in the first dropdown is in the POST array

                       true // close tag

                       );

            foreach ($listaDeCidades as $cidade){

               echo CHtml::tag(

                       'option', // tagname

                       array('value'=>$cidade->id), // html params of tag

                       $cidade->nome, // value from the item selected in the first dropdown is in the POST array

                       true // close tag

                       );

               }

        }

How can i get a list of cities of the country $model_estado->id and how can I select the id of the model_cidade in a dropdown?