yii2yii2-advanced-appyii2-modelyii2-validation

how to keep selected value on Yii2 html::dropddownlist?


i have the following code in my view:

<?=Html::dropdownList('region',null,
ArrayHelper::map(Ethioregion::find()->all(),'region','region'),[
        'prompt' => 'Select Region..','style'=>'width:200px',]) ?>
and submit button

 <?= Html::submitButton('Search', ['name' => 'dele','class' => 'btn btn-primary']) ?>

my code working well but I have one big problem. I select a region and click the search button, the result comes correctly, and 10 result is shown per page when I click the next page, it gives me false data, I notice this is due to the selected value on the dropdownlist is reset. I tried both making " Selected'=> true, and 'Selected'=>'selected', but it does not work. so how can I make the selected value on dropdownlist and checkboxlist will be selected after the page is loading? thanks


Solution

  • It is very simple, you must pass integer $region_id only:

    <?=Html::dropdownList('region', $region_id,
    ArrayHelper::map(Ethioregion::find()->all(),'region','region'),[
            'prompt' => 'Select Region..','style'=>'width:200px',]) ?>