yii下拉列表值未设置为db
我的2个相关下拉列表检查类型和状态正常,它们显示值。但这些值未设置到数据库中。请帮助我…这是我的代码 表单视图:yii下拉列表值未设置为db,yii,Yii,我的2个相关下拉列表检查类型和状态正常,它们显示值。但这些值未设置到数据库中。请帮助我…这是我的代码 表单视图: 这很可能是下拉列表名称中的问题。你应该替换 CHtml::dropDownList('exam_type' 与 及 与 几天前,我也陷入了这个问题。我做了一些技巧来解决这个问题 <tr> <td><?php echo $form->labelEx($model,'exam type :'); ?></td> <t
这很可能是下拉列表名称中的问题。你应该替换
CHtml::dropDownList('exam_type'
与
及
与
几天前,我也陷入了这个问题。我做了一些技巧来解决这个问题
<tr>
<td><?php echo $form->labelEx($model,'exam type :'); ?></td>
<td>
<?php echo CHtml::dropDownList('exam_type','',CHtml::listData(class1::model()->findAll(),'class','class'),array('empty'=>'Choose one',
'ajax' => array(
'type'=>'POST', //request type
'url'=>CController::createUrl('dynamicstates'), //url to call.
//Style: CController::createUrl('currentController/methodToCall')
// 'update'=>'#status', //selector to update
'update'=>'#modelname_status', //here is the trick replace modelname to orginal model class name
)));
//empty since it will be filled by the other dropdown
?>
</td>
<td> <?php echo $form->error($model,'exam_type'); ?></td>
</tr>
<tr>
<td><?php echo $form->labelEx($model,'status :'); ?></td>
<?php //echo CHtml::dropDownList('status','', array());?>
<td><?php echo CHtml::dropDownList('modelname[status]','', array());?></td>//same here
<td> <?php echo $form->error($model,'status'); ?></td>
</tr>
//这里也一样
在上述代码中,我更改了:
'update'=>'#modelname_status', //here is the trick replace modelname to orginal model class name
<td><?php echo CHtml::dropDownList('modelname[status]','', array());?></td>//same here
'update'=>'#modelname_status',//下面是将modelname替换为原始模型类名的技巧
//这里也一样
即使它不起作用,也要发表评论。(我没有测试,也许你的控制器有问题。)我希望你的问题已经解决了。如果有人仍然想知道为什么这些下拉列表不更新数据库,那么它们不会更新,因为它们没有列为安全属性。 下面是一个使下拉列表起作用的示例(仅给出了重要部分): 假设要添加名为“showOnHome”的字段。首先,在数据库表中创建一个名为“showOnHome”的字段(我更喜欢int(1)作为字段类型) 然后,在您的模型中:
public function rules() {
....
....
array('showOnHome','safe'),
}
....
....
public function attributeLabels() {
return array(
.....
.....
.....
'showOnHome' => 'Show On Homepage' ,
)
}
<div class="row">
<?php echo $form->labelEx($model,'showOnHome'); ?>
<?php echo $form->dropDownList($model, 'showOnHome', array('0'=>'No','1'=>'Yes') , array('empty'=>'--Select--')); ?>
<?php echo $form->error($model,'showOnHome'); ?>
</div>
在您看来:
public function rules() {
....
....
array('showOnHome','safe'),
}
....
....
public function attributeLabels() {
return array(
.....
.....
.....
'showOnHome' => 'Show On Homepage' ,
)
}
<div class="row">
<?php echo $form->labelEx($model,'showOnHome'); ?>
<?php echo $form->dropDownList($model, 'showOnHome', array('0'=>'No','1'=>'Yes') , array('empty'=>'--Select--')); ?>
<?php echo $form->error($model,'showOnHome'); ?>
</div>
提交时是否填充了您的
$\u帖子
?表单是否有效?当我们单击“提交”按钮时,考试类型和状态值未发送到db。其显示不能为空。提交时是否填充了您的$\u POST
?否。chtml是否有任何问题?否,未填充。它不工作。。谢谢你,我一直在寻找解决我问题的方法,在你的答案上找到了。非常感谢。
'update'=>'#modelname_status', //here is the trick replace modelname to orginal model class name
<td><?php echo CHtml::dropDownList('modelname[status]','', array());?></td>//same here
public function rules() {
....
....
array('showOnHome','safe'),
}
....
....
public function attributeLabels() {
return array(
.....
.....
.....
'showOnHome' => 'Show On Homepage' ,
)
}
<div class="row">
<?php echo $form->labelEx($model,'showOnHome'); ?>
<?php echo $form->dropDownList($model, 'showOnHome', array('0'=>'No','1'=>'Yes') , array('empty'=>'--Select--')); ?>
<?php echo $form->error($model,'showOnHome'); ?>
</div>
/**
* Creates a new model.
* If creation is successful, the browser will be redirected to the 'view' page.
*/
public function actionCreate()
{
$model=new Livefuture;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['Livefuture']))
{
$model->attributes=$_POST['Livefuture'];
if($model->save())
$this->redirect(array('view','id'=>$model->id));
}
$this->render('create',array(
'model'=>$model,
));
}
/**
* Updates a particular model.
* If update is successful, the browser will be redirected to the 'view' page.
* @param integer $id the ID of the model to be updated
*/
public function actionUpdate($id)
{
$model=$this->loadModel($id);
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['Livefuture']))
{
$model->attributes=$_POST['Livefuture'];
if($model->save())
$this->redirect(array('view','id'=>$model->id));
}
$this->render('update',array(
'model'=>$model,
));
}