如何使控制器记住ajax发布数据Yii2

时间:2015-04-02 08:10:31

标签: jquery ajax yii2

我有多个模态窗口,从其中一个我想发送几个$模型到另一个窗口它像父亲一样少儿子......我们不能保存儿子因为我们不知道父亲的身份和名字。

JS

$("#modal-deposit").submit(function($form) {

    $form.preventDefault();
    $.ajax({
        type: "POST",
        url: "index.php?r=family/create", // сreatedeposit создает запись.
        data: $("#form-deposit").serialize(), // Сериализует the form's elements.
 return false;
});

动作

public function actionCreate()
    {
        $family_model = new family();
        $model_deposit = new FinanceIncomeExpenses();

        if ($family_model->load(Yii::$app->request->post())
            && $family_model->save())
        {
           if ($model_deposit->sum != null)// one of the required fields
            {
               $model_deposit->family_id = $family_model->id;
               $model_deposit->save();
            }


        } else {
            if(Yii::$app->getRequest()->isAjax){
                return $this->renderAjax('create', [
                    'family_model' => $family_model,
                    'model_deposit' => $model_deposit,
                ]);
            }else{
                return $this->render('create', [
                    'family_model' => $family_model,
                    'model_deposit' => $model_deposit,
                ]);
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

您需要更改

url: "index.php?r=family/create",

url: "<?php echo \Yii::$app->getUrlManager()->createUrl('family/create') ?>",