Yii2 dropdown selected value

Muhammad Shahzad picture Muhammad Shahzad · Dec 20, 2014 · Viewed 48.2k times · Source

I want to show selected value in Yii2 dropdown,

$_GET Value:

  $id = $_GET["cid"];

Drop down code

  $form->field($model, 'userid')
    ->dropDownList(
          [User::getUser()],
          //[ArrayHelper::map(User::findAll(['active' => '1']), 'id', 'name')],
          ['prompt'=>'Select a user','id'=>'user_dropdown'],    
          ['options' =>
                    [                        
                      $id => ['selected' => true]
                    ]
          ]

        )->label('');           

but this method is not working!

Answer

Nakarin Kongsumrit picture Nakarin Kongsumrit · Dec 20, 2014

Try this.

$model->userid=$id;
$form->field($model, 'userid')
->dropDownList(...)
->label('');