Need your help. I have a dependent drop downlist City that gets populated when user selects a State. Everything works great in create action. However, in the default update action the State dropdown gets populated with user submitted value however the City(dependent dropdown) remains blank. If select a different state then my city dropdown starts working again. How do I fix this without upsetting the working create action.
_form.php
<div class="row">
<?php echo $form->labelEx($model,'state'); ?>
<?php echo $form->dropDownList($model,'stateId',CHtml::listData(State::model()->findAll(), 'id', 'name'), array(
'ajax' => array(
'type'=>'POST', //request type
'url'=>CController::createUrl('city'), //url to call.
//Style: CController::createUrl('currentController/methodToCall')
'update'=>'#' . CHtml::activeId($model, 'cityId'), //selector to update
//'data'=>'js:javascript statement'
//leave out the data key to pass all form values through
))); ?>
<?php echo $form->error($model,'stateId'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'city'); ?>
<?php echo $form->dropdownList($model,'cityId',array()); ?>
<?php echo $form->error($model,'cityId'); ?>
</div>
controller.php controller name is Ticket
public function actionUpdate($id)
{
$model=$this->loadModel($id);
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['Ticket']))
{
$model->attributes=$_POST['Ticket'];
if($model->save())
$this->redirect(array('view','id'=>$model->id));
}
$this->render('update',array(
'model'=>$model,
));
}
public function actionCity()
{
$data=City::model()->findAll('stateId=:id',
array(':id'=>(int) $_POST['Ticket']['stateId']));
$data=CHtml::listData($data,'id','name');
foreach($data as $value=>$name)
{
echo CHtml::tag('option',
array('value'=>$value),CHtml::encode($name),true);
}
}