我正在尝试在_form视图中创建dropDownList。
这是控制器部分
public function actionCreate()
{
$modelCountry = Country::model()->findAll();
$this->render('create',array(
'model'=>$model,'modelCountry'=>$modelCountry,
));
}
现在在_form视图中我有一个TextField,必须修改为dropDownList
<div class="row">
<?php echo $form->labelEx($model,'name_en'); ?>
<?php echo $form->textField($model,'name_en',array('size'=>60,'maxlength'=>255)); ?>
<?php echo $form->error($model,'name_en'); ?>
</div>
这意味着我必须将其修改为类似
的内容<?php echo $form->dropDownList($model,'name_en',array('!Variable!A!From_modelCountry!','!Variable!B!From_modelCountry!' )); ?>
我可以使用foreach循环并在该循环内部创建一些DropDownList但是可能有更好的方法从数组$ modelCountry中取出数据并将它们直接放到该dropDownList的数组中?
答案 0 :(得分:1)
$list=CHtml::listData($modelCountry,'id','name_en');
$this->render('create',array(
'model'=>$model,'modelCountry'=>$modelCountry,'list'=>$list
));
现在在你的视图中你可以写
<?php echo $form->dropDownList($model,'name_en',$list); ?>
您可以在视图中使用Chmlt :: listData,但这应该是控制器的一部分,这就是MVC的全部内容。
答案 1 :(得分:1)
<?php echo $form->dropDownList($model,'name_en',CHtml::listData(Country::model()->findAll(),'id','name')); ?>