Dependent Dropdown Empty When Validation Error

hi to all, i have 2 dependent drop, its work ok, when i select on the first field and its show on on the second field, but the problem is when error validation, tmy dependent dropdown return to empty

this is my controller code on my dependent


 public function actionRoom()

	{


		$sched = Schedule::model()->findByPk($_POST['MonitoringHdr']['sched_id']);

		$data = array();

		if($sched != NULL){

			 $data=Room::model()->findAll('bldg_id=:bldg_id', 

	                  array(':bldg_id'=>$sched->bldg_id));

			 $data=CHtml::listData($data,'id','room');

		}else{

			echo CHtml::tag('option',

	                   array(),"No rooms available",true);

		}

	  	  

	    foreach($data as $value=>$name)

	    {

	        echo CHtml::tag('option',

	        array('value'=>$value),CHtml::encode($name),true);

	    }

	}

my view


'ajax' => array(

                'type'=>'POST', //request type

                'url'=>CController::createUrl('MonitoringHdr/room'), //url to call.

                //Style: CController::createUrl('currentController/methodToCall')

                'update'=>'#room_id', //selector to update

               

                )));?>


echo CHtml::dropDownList('room_id','', array());

hi

you can write main action??

Your code like this ?? :





public function actionExm() {

        $model = new modelName;


        

        $this->performAjaxValidation($model);


        if (isset($_POST['modelName'])) {

.

.

.


}

.

.

}



thank you for reply sir, i already enable that on my main action which is actionCreate.

try this :




public function actionCreate() {

        $model = new ConContract;


      

        $this->performAjaxValidation($model);


        if (isset($_POST['ConContract'])) {

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

            $model->room_id = $_POST['ConContract']['room_id'];

.

.

.


}

}



if getting error on validation after selecting dropdown and get empty when page get reload so check the room_id value for !empty() and give the data array to this dropdown instead of empty array().




echo CHtml::dropDownList('room_id','', array());



all that can be simplified


<?php


// controller

public function actionRoom()

{

    $sched = Schedule::model()->findByPk($_POST['MonitoringHdr']['sched_id']);

    if ($sched != NULL) {

        $data = Room::model()->findAllByAttributes('bldg_id' => $sched->bldg_id]);

        echo CHtml::listData($data, 'id', 'room');

    }

}


// view

echo CHtml::dropDownList('room_id', '', ['No rooms available']);