Delete button in CGrid view in Yii

I tried to create a Delete button in CButtonColumn,The problem is when I triggered the delete,It shows me an error Your request is invalid…Can anyone tell me why this is happening…

My controller

public function actionDelete($id)

{


    $this->loadModel($id)->delete();





    // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser


    if(!isset($_GET['ajax']))


        $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));


}

And my view

<?php $this->widget('zii.widgets.grid.CGridView', array(


'id'=>'replay-comment-grid',


'dataProvider'=>$model->search(),


// 'filter'=>$model,


'columns'=>array(


    //'id',


    //'comment_id',


    'admin_replay',


    array(


            'class' => 'CButtonColumn',


            'htmlOptions'=>array('width'=>'180px'),


            'template' => '{view} {update} {delete}',


            'buttons' => array(


                    'view'=>array(


                        'imageUrl' =>false,


                        'label' => 'View', 


                        'url'=>'Yii::app()->createUrl("replayComment/view/$data->id")',


                        'options' => array('title'=>'view','class'=>'btn btn-success btn-xs'),


                    ),


                    'update'=>array(


                        'imageUrl' =>false,


                        'label' => '',


                        'url'=>'Yii::app()->createUrl("replayComment/update/$data->id")',


                        'options' => array('title'=>'update','class'=>'btn btn-info btn-xs fa fa-pencil-square-o'),


                            ),    


                    'delete'=>array(


                        'imageUrl' =>false,


                        'label' => 'delete', 


                        'url'=>'Yii::app()->createUrl("replayComment/delete/$data->id")',


                        'options' => array('title'=>'delete','class'=>'btn btn-danger btn-xs'),


                    ),                                  


            )


        ),


),


'itemsCssClass'=>'table table-striped table-bordered table-hover',


    'pagerCssClass'=>'pagination', 


    'pager'=>array( 'header' => '','lastPageLabel'=>'<span class="glyphicon glyphicon-chevron-right"></span><span class="glyphicon glyphicon-chevron-right"></span>','firstPageLabel'=>'<span class="glyphicon glyphicon-chevron-left"></span><span class="glyphicon glyphicon-chevron-left"></span>','prevPageLabel'=>'<span class="glyphicon glyphicon-chevron-left"></span>','nextPageLabel'=>'<span class="glyphicon glyphicon-chevron-right"></span>','header' => '','cssFile' => Yii::app()->baseUrl . '/css/pager.css','htmlOptions'=>array('class'=>'pagination'),'selectedPageCssClass'=>'active'),

)); ?>

Good morning Mohammed Iqbal Khan, you can try with exceptions in the controller en el action delete, so:




public function actionDelete($id)

	{

		try{

			$this->loadModel($id)->delete();


			// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser

			if(!isset($_GET['ajax']))

				$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));

		} catch(CDbException $e){

	        if($e->getCode()===23000){

	            //You can have nother error handling

	            header("HTTP/1.0 400 Relation Restriction");

	            echo 'Este registro no puede ser borrado, porque está relacionado con información importante de la base de datos';

	        }else{

	            throw $e;

	        }

		}

	}



or you can quit the buttons oin the view admin, so:




	array(

			'class'=>'CButtonColumn',

			'template'=>'{update}{view}{delete}',

            'buttons'=>array(

                'update'=>array(

                        'visible'=>'false',

                ),

                'view'=>array(

                        'visible'=>'true',

                ),

                'delete'=>array(

                        'visible'=>'false',

				),

			),

		),



I hope to help you

In your routes, make sure you have a rule that allows replayComment/update/$data->id point to replayComment/update/id/$data->id

Check your top of the controller, By default delete action are not worked as GET Method, you can use POST method.