yii2删除GridView时:无法验证您的数据提交



当我想删除GridView上的一个项目时,我得到这个错误:

exception 'yiiwebBadRequestHttpException' with message 'Unable to verify your data submission.

My this My controller code:

class DevisController extends Controller
{
 public $layout = 'lay-admin';
 public function behaviors()
 {
    return [
        'verbs' => [
            'class' => VerbFilter::className(),
            'actions' => [
                'delete' => ['post'],
            ],
        ],
    ];
}
/* ..... */
public function actionDelete($id)
{
    $this->findModel($id)->delete();
    return $this->redirect(['index']);
}

当我将行为函数中的post方法改为get方法时就会出现这个错误

Method Not Allowed. This url can only handle the following request methods: GET.
GridView代码:

<?= GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,
                'columns' => [
                    ['class' => 'yiigridSerialColumn'],
                    //'idDevis',
                    'reference',
                    'client',
                    'dateCreation',
                    'contact',
                    'valableJusqua',
                    'dateRelance',
                    [
                     'attribute'=>'etat',
                     'filter'=>ArrayHelper::map(Devis::find()->asArray()->all(), 'etat', 'etat'),
                    ],
                    'commercial',
                    'modePaiement',
                    'delaiPaiement',
                    ['class' => 'yiigridActionColumn'],
                ],
]); ?>

有什么想法吗?

在自定义布局文件中添加CSRF元标记。

的例子:

<?php $this->beginPage() ?>
<!DOCTYPE html>
<html lang="<?= Yii::$app->language ?>">
<head>
    <meta charset="<?= Yii::$app->charset ?>">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <?= Html::csrfMetaTags() ?>
    <title><?= Html::encode($this->title) ?></title>
    <?php $this->head() ?>
</head>
<body>
<?php $this->beginBody() ?>

最新更新