Dropdownlist

Issue, I have an array with a list of cities. However, after I sort it and display it in the dropdownlist and click submit. My city column in the database gets populated with index of the array i.e 0 or 1 or 2. Even though in the form dropdownlist I see London, New Jersey or Chicago. I would like to save the city name in my database and not the index.

Could you suggest how I could do this by referring to the code below.

This the _form.php code

<div class="row">


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


	<?php echo $form->dropDownList($model,'city',$model->getCityOptions()); ?>


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


</div>

This is Model code

public function getCityOptions()

{


	$list=array('New Jersey','Chicago','London',);


 	asort($list);


 return $list;


}

Dear Friend,

Array values are displayed and array keys are stored.

Try this.




public function getCityOptions()

{

   $list=array('New Jersey'=>'New Jersey','Chicago'=>'Chicago','London'=>'London',);

   asort($list);

   return $list;

}



Thanks! nice hack :slight_smile: