2016-03-28 63 views
1

Yii2刪除確認對話框不能通過菜單窗口項目工作。Yii2方法不允許。此URL只能處理以下請求方法:POST

[ 
    'label' => '<i class="fa fa-trash-o alis"></i> Sil', 
    'url' => ['site/delete', 'id' => $model->id], 
    'linkOptions' => [ 
     'data-confirm' => 'Are you sure you want to delete this item?', 
     'data-method' => 'post', 
    ], 
    'visible' => 'visible' 
], 

而且我看到這個錯誤:

Method Not Allowed (#405) Method Not Allowed. This url can only handle the following request methods: POST.

如何使用刪除確認對話框。然後我嘗試這一點,但沒有工作...

[ 
    'label' => '<i class="fa fa-trash-o alis"></i> delete', 
    'url' => ['site/delete','id' => $model->id], 
    [ 
     'data' =>[ 
      'data-confirm' => 'Are you sure you want to delete this item?', 
      'data-method' => 'post', 
     ], 
    ], 
    'visible' => 'visible' 
], 

回答

1

我通過模板選項的固定問題,如下面的代碼塊:

['label' => '<i class="fa fa-trash-o alis"></i> delete', 
    'url' => ['site/delete','id' => $model->id], 
    'template' => '<a href="{url}" data-confirm = "Are you sure you want to delete this item?", data-method="post">{label}</a>', 
    'visible' => 'visible' 
], 
0

在視圖文件

['label' => '<i class="fa fa-trash-o alis"></i> delete', 
    'url' => ['site/delete','id' => $model->id], 
    'template' => '<a href="{url}" data-confirm = "Are you sure you want to delete this item?", data-method="post">{label}</a>', 
    'visible' => 'visible' 

],

而且在AppAsset文件中,我們必須依賴這樣的數組:

public $depends = [ 
     'yii\web\YiiAsset', 
     'yii\bootstrap\BootstrapAsset', 
]; 
0

您發送並GET。但是,在默認情況下刪除控制器 - 只發布 發送POST或編輯規則位指示,就像這樣:

public function behaviors() 
{ 
    return [ 
     'verbs' => [ 
      'class' => VerbFilter::className(), 
      'actions' => [ 
       'delete' => ['post'], //delete this string to may GET 
      ], 
     ], 
    ]; 
} 
0

我認爲錯誤的原因是HTTP方法,當你點擊與方法,你鏈接的區別配置用於控制器的函數行爲()中的操作。所以你需要定義鏈接的方法(對不起,我的英文不好)。我試過了,它的工作原理如下:

Html::a('', $url, 
    [ 
    'data' => [ 
     'method' => 'post', 
      // use it if you want to confirm the action 
      'confirm' => 'Are you sure?', 
     ], 
     'class' => 'glyphicon glyphicon-trash btn btn-default btn-xs custom_button' 
    ] 
); 
相關問題