简体   繁体   中英

Changing the model without redirecting to the update page

In my controller there's the update method, I want it like this, If I click the update button the $model->status = ('Done'); without redirecting it to the update page.

What I have tried so far, the page refreshes but the $model->status doesn't change into 'Done'

 public function actionUpdate($id)
{
    $model = $this->findModel($id);

    if ($model->load(Yii::$app->request->post()) && $model->save()) {
       return $this->redirect(['view', 'id' => $model->id]);
    } else {
        $model->status = ('Done');
        $model->time_end = date('y-m-d h-i-s');
        return $this->redirect(['view', 'id' => $model->id]);

    }
}

Nevermind I've already found the solution:

public function actionUpdate($id)
{
    $model = $this->findModel($id);
    $model->status = ('Done');
    $model->time_end = date('y-m-d h-i-s');

    if ($model->load(Yii::$app->request->post()) && $model->save()) {
       return $this->redirect(['view', 'id' => $model->id]);
    } else {
        return $this->render('view', [
            'model' => $model,
        ]);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM