2016-10-15 89 views
0

我剛剛啓動Yii2框架。
我想創建一個1到10的下拉列表和一個提交按鈕
一旦選擇了該選項並點擊按鈕應該轉到下一頁來顯示我選擇的數字。yii2:未定義變量:model

在我看來文件:在下拉列表中有:index.php文件

use yii\widgets\ActiveForm; 
<?php $form = ActiveForm::begin(); ?> 
    <?= $form->field($model, 'QTY')->dropDownList(range(1, 10)) ?> 
    <?= Html::submitButton('Buy', ['class' => 'btn btn-primary']) ?> 
<?php ActiveForm::end(); ?> 

然後當我去它給我的頁面「的模式未定義的變量」。

我該怎麼做才能使其正確?

Html和CHtml有什麼不同?

謝謝。

+1

'Activeform'中使用的字段應該與'model'關聯。顯示呈現視圖的控制器動作。 –

+0

嗨,謝謝,我已經發現了錯誤。 –

回答

0

此代碼是form.php而不是index.php。因爲我們可以看到,有積極的形式。 你的模型是不確定的,也許你寫了錯誤的代碼

這是典型控制器的index.php

public function actionIndex() 
 
    { 
 
     $searchModel = new PersediaanBarangSearch(); 
 
     $dataProvider = $searchModel->search(Yii::$app->request->queryParams); 
 

 
     return $this->render('index', [ 
 
      'searchModel' => $searchModel, 
 
      'dataProvider' => $dataProvider, 
 
     ]); 
 
    }

的HTML和cHTML是一樣的Yii1 =了CHtml 在Yii2 = HTML

0

這是要分頁?如果是使用網格視圖的默認功能。

這正好控制器

$query = Post::find()->where(['status' => 1]); 

$provider = new ActiveDataProvider([ 
    'query' => $query, 
    'pagination' => [ 
    'pageSize' => 10, 
    ], 
    'sort' => [ 
    'defaultOrder' => [ 
     'created_at' => SORT_DESC, 
     'title' => SORT_ASC, 
    ] 
], 
]); 
return $this->render('path_to_view',['dataProvider'=>$provider]); 

Read more

這正好視圖

GridView::widget([ 
    'dataProvider' => $dataProvider, 
    'columns' => [ 
    'id', 
    'name', 
    'created_at:datetime', 
    // ... 
    ], 
]); 

Read more

0

其實你的模型沒有加載,請檢查下面的例子。

public function actionIndex($id = Null) 
    { 
     $data=array(); 
     $data['model'] = !empty($id) ? \app\models\YourModel::findOne($id) : new \app\models\YourModel(); 

     return $this->render('index', $data); 
    }