php - 在 View 中渲染数据时出现"Undefined variable"错误

标签 php view yii2

我想在 _form.php View 中渲染静态数组,使用 Yii2 中的 actionCreate 方法。这是我的代码:

if ($model->load(Yii::$app->request->post()) && $model->save()) {
     return $this->redirect(['view', 'id' => $model->id]);
} else {
    $data = array('1'=>'AA','2'=>'BB');

    return $this->render('create', [
        'model' => $model,
        'data' => $data ,
    ]);
}

当我尝试在 _form.php View 中显示此数据时,收到错误“ undefined variable :数据”。

这是我的_form.php代码:

<?= $form->field($model, 'fixer_type[]')->dropDownList($data,['prompt'=>'Select Fixer Trade']) ?>

我错过了什么?

最佳答案

您正在将 $data 传递给 create.php。从 create.php 渲染相同的内容,使其在您的 form.php.

在你的create.php

<?= $this->render('_form', [
    'model' => $model,
    'data' => $data ,
]) ?>

关于php - 在 View 中渲染数据时出现"Undefined variable"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30909115/

相关文章:

PHP调试preg-replace回调函数

MySQL View 优化

php - Yii2 ActiveDataProvider 通过 ActiveQueryInterface 查询(通过关系查询)

javascript - javascript 数组中有新行时出错

android - 设计TextView和ImageView的 View 组合

php - 考虑用户访问级别,为聊天室应用程序中的每个用户分配空间

php - 如何计算Paypal付款费用

php - View 中 undefined variable taskid

php - 删除foreach中的重复项

c# - 'CREATE VIEW' 必须是查询批处理中的第一个语句