在yii控制器中删除功能
问题描述:
How do i get the delete button to work in Yii? These are my controller and view and i have a custom delete button.
public function actionDelete($id)
{
$model = $this->loadModel($id);
$model->DeletedItem = 1;
$model->save();
echo"success";
if(!isset($_GET['ajax']))
$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
}
and my view is:
function Delete(Iid,Itype){
if(type=="a")
{
console.log("Assignment");
$.ajax({
type: 'GET',
//data: Iid,
url: '/grade/delete/' + Iid,
success: function (data) {
console.log(data);
//$.fn.yiiGridView.update('Assignment-grid');
}
});
答
Normally in yii1 for delete you should do this way (using $model->delete();)
public function actionDelete($id)
{
$model = $this->loadModel($id);
//$model->DeletedItem = 1;
$model->delete();
// $model->save();
echo"success";
if(!isset($_GET['ajax']))
$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
}
For the view custom delete botton could be a path problem
try assign the routed path this way
url: <?php echo "'" . Url::to(['grade/delete/']) ."'" ?> + Iid,