protected function afterSave($model, $wasNew)
    {
        if ($wasNew) {
            $message = Yii::t('app', '<strong>Success!</strong> A new record has been created.');
        } else {
            $message = Yii::t('app', '<strong>Success!</strong> Record has been updated.');
        }

        if (!isset($_POST[self::NEW_RELATED_BUTTON_NAME])) {
            $this->setFlash('success', $message);
        }

        $id = $this->exportKey($model->getPrimaryKey(true));
        $response = Yii::$app->getResponse();
        $response->setStatusCode(201);

        if (!Yii::$app->request->isAjax) {
            // when such header is set, ActiveController will change the response code set above to a redirect
            $response->getHeaders()->set('Location', isset($_POST[self::NEW_RELATED_BUTTON_NAME])
                ? Url::current(['id' => $id, self::ADD_RELATED_NAME => $_POST[self::NEW_RELATED_BUTTON_NAME]], true)
                : Url::toRoute([$this->viewAction, 'id' => $id], true));
        }

        $response->getHeaders()->set('X-Primary-Key', $id);
    }