DropdownList Values Not Saved in Controller?

At my _form.php i have


	<div class="row">

		<?php

			echo $form->labelEx($model, 'Fruits');

			$FruitsArr = array

			(

				0 => 'Apple',

				1 => 'Orange',

			);			

			echo CHtml::dropDownList('Fruits', $model->Fruits, $FruitsArr); 

		?>

	</div>

at FruitsController.php i have


	public function actionCreate()

	{

		$model=new Fruits;


		// Uncomment the following line if AJAX validation is needed

		// $this->performAjaxValidation($model);


		if(isset($_POST['Fruits']))

		{

			$model->attributes=$_POST['Fruits'];

			echo $model->Fruits;

		}


		$this->render('create',array(

			'model'=>$model,

		));

	}

The echo value of $model->Fruits always return 0 even if i select Orange.

Not sure what’s wrong :(

you have to save model, i.e: [b]model->save()

[/b]…

after that print model value.

Alright, thought $model->attributes=$_POST[‘Fruits’] will assign values into the model :rolleyes:

I notice there is a strange effect on the dropdownlist if there is validation errors like required field not filled in. Say i select Orange then click submit, a validation error return due to one of the required field not filled in. I would expect the selected value of Orange is preserved but it changed back to Apple!?

Other text field values were preserved properly but dropdown were not. How do i make dropdown preserve previous values when validation error?

Ok found the problem

Need to use


echo $form->dropDownList($model, 'Fruits', $FruitsArr);

instead of


echo CHtml::dropDownList('Fruits', $model->Fruits, $FruitsArr);

I think yii documentation seriously needs a working example for every module & function much like MSDN.