如何在yii2中显示gridview

The following code is used to display gridview but it shows "data provider" error.

MODELS:

Subcategory.php:

public function getCountries()
 {
     return $this->hasOne(Subcategories::className(),['id'=>'catid']);
 }

VIEW:

subcategory.php: i have added this following coding to view the saved data in a grid view.

<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns'=>[
    ['class'=>'yii\grid\SerialColumn'],
    'id',
    'categoryname',
    [
        'attribute' => subcategory,
        'value'=>getCountries.subcatname,
    ],
    ['class'=>'yii\grid\ActionColumn'],
],
]); ?>

CONTROLLER

public function actionView2()
{
  $dataProvider = new ActiveDataProvider([
      'query' => Subcategory::find(),
      'pagination' => [
          'pageSize' => 20
       ]
   ]);
   $posts = $dataProvider->getModels();
   return $this->render('subcategory' [
       'dataProvider' => $dataProvider,
   ]);
}

I have applied this coding but I get the following error:

PHP Notice – yii\base\ErrorException Undefined variable: dataProvider