Dropdownlist

I want to show a selected value from the table in the dropdownlist as the starting value like the "prompt" statement.

When i am displaying using prompt,then on sending the form, the value is not set and its storing blank.If i am selecting another value, then its working fine.

If i am using value instewad of prompt,its not showing selected value.Please help me.

Here $model is my model, $modellist is the list.

<?php echo $form->dropDownList($model, ‘id’, CHtml::listData($modellist, ‘id’, ‘name’), array(‘value’ =>(isset($model->name))?$model->name:“Select”)); ?>

can you attache screen shot of what you want to do?

If I understand the question:


echo $form->dropDownList($model, 'IdMotivo', CHtml::listData(

                       ToMotivosIngreso::model()->findAll(), 'IdMotivo', 'Motivo'),

                       array('options'=>array(2=>array('selected'=>'selected')))); 

These codes do the item number 2 (by example, you can change it) on the list is the default item.

Regards.

You don’t have to set the value explicit, this should be done by Yii when the $model->id is set to a value from your listData keys.

Are you sure the $model->id is set when rendering the dropdownlist?

Check the values.





var_dump($model->attributes); //$model->id

var_dump(CHtml::listData($modellist, 'id', 'name')); 


echo $form->dropDownList($model, 'id', CHtml::listData($modellist, 'id', 'name'), array('prompt'=>'Select'));




I am getting the value, suppose a user has saved some data in a table. And later he wants to edit the data.When he is editing the data, i want to show him the previous value selected by him in the dropdownlist as the first option in place of "select".

In my code previous selected value is not shown on the list.

I am getting the value along with the id.

Suppose a user has saved some data in a table. And later he wants to edit the data.When he is editing the data, i want to show him the previous value selected by him in the dropdownlist as the first option in place of "select".

In my code previous selected value is not shown on the list.

The value shown in the screen should be the value stored (previously selected) in the database.

When a user returns to the screen, that value should be shown. No reason to put it on the first value in the list.

yeah you are right…but the question is how to get the previous selected value…??