如何将参数从创建表单发送到yii2中的Action

时间:2014-08-19 12:24:30

标签: yii2

好的,我正在尝试制作调查工具。

所以,我有一个参考N个问题的调查模型。这是该模型的一部分:

<?php
namespace app\models;
use Yii;

/**
 * @return \yii\db\ActiveQuery
 */
public function getIdmateria0()
{
    return $this->hasOne(Monmateria::className(), ['id' => 'idmateria']);
}

/**
 * @return \yii\db\ActiveQuery
 */
public function getMonpreguntas()
{
    return $this->hasMany(Monpregunta::className(), ['idencuesta' => 'id']);
}
}

当然,问题的模型Monpregunta参考了调查的模型。然后我在调查和他的问题之间有1到N的关系。

在我的ActionView中,我有一个关于它的N个问题的调查。然后我调用一个调查视图,在其上呈现创建表单视图,以便创建调查的N个答案。所以,一旦回答问题,我需要知道问题的数量。我需要跟踪我正在响应的问题数量,但是对于它我需要从表单向我的控制器发送一个参数。但我不知道该怎么做。

控制器(这里我需要跟踪哪些问题得到回复)

public function actionView($id)
{
    //this is my id's survey       
    $model = $this->findModel($id);        
    //here i pre create my answer 
    $modelResultado = $this->crearResultado($id);        
    //here i call the view with a Create Form embedded. When the form action it's triggered  
    return $this->render('view', [
        'model' => $model,
        'modelResultado'=>$modelResultado,
        'orden'=>$model->orden,
    ]);
}
//create the answer and setting the reference
protected function crearResultado($id) {
    $modelResul = new \app\models\Monresultado;
    $modelResul->idmonresultadocab = $id;
     if ($modelResul->load(Yii::$app->request->post()) && $modelResul->save()) {
        //flash
    }
    return $modelResul;
}

视图

<?php

use yii\helpers\Html;
use yii\widgets\DetailView;

/**
 * @var yii\web\View $this
 * @var app\models\Monresultadocab $model
 */


?>
<div class="monresultadocab-view">


<?= DetailView::widget([
    'model' => $model,
    'attributes' => [
        'id',
        'idencuesta',
    ],
]) ?>
<?php
echo $this->render('_formResultado', [
    'model' => $modelResultado
  ]); ?>
</div>

[b]这是_formResultado视图,在这里我想告诉我们哪些问题已被回复,但我不知道如何将参数发送到动作!! [ / b]

<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\helpers\ArrayHelper;

/**
 * @var yii\web\View $this
 * @var app\models\Monresultado $model
 * @var yii\widgets\ActiveForm $form
 */
?>

<div class="monresultado-form">

<?php $form = ActiveForm::begin(); ?>


<?= $form->field($model, 'idrespuesta')->dropDownList(ArrayHelper::map(\app\models\Monrespuesta::find()->all(),'id','nombre'))?>           



<?= $form->field($model, 'libre')->textInput(['maxlength' => 2000]) ?>

<div class="form-group">
    [b]<?= Html::submitButton($model->isNewRecord ? 'Siguiente' : 'Siguiente', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>[/b]
</div>

<?php ActiveForm::end(); ?>

</div>

如果我在Html :: submitButton标记中发送参数,它就不起作用。

由于

1 个答案:

答案 0 :(得分:0)

我想你应该在控制器的actionView中传递相应问题的id  像:

   return $this->render('view', [
    'model' => $this->findModel($id), 
    ......
    ......
     ]);

希望它有效!

相关问题