如何在Yii2中的控制器中发送模型值

时间:2017-11-10 13:37:20

标签: php gridview yii2 yii2-model yii2-grid

我有一个视图,其中我使用了GridViewDetailView。在我的控制器中,我通过它的ID发送模型。因此,通过这种方式我的GridView正在运作。

控制器1

public function actionViewcreated($id)
{
         $model=$this->findModel($id);//the id of created form
    /*print_r($model);
    exit();*/
    $id = $model->id;
    $created_by = $model->created_by;
    $issuer = $model->issuer;
    $store = $model->store_id;
    $admin = $model->admin_incharge;
    $pm = $model->project_manager;

   .....
    // my other code

    return $this->render('viewcreated', [
        'dataProvider' => $dataProvider,
        'model' => $this->findModel($id),
        'id'=> $model->id
        /*'searchModel' => $searchModel*/
    ]);


}

查看

$this->title = $model->id;
$this->title = 'Outward Gate Pass';
$this->params['breadcrumbs'][] = $this->title;
.
.
.
.
 <?= DetailView::widget([
            'model' => $model,
            'attributes' => [
                //'id',
                [
                        'label'=>'OGP Serial #',
                         'value' => function($d)
                         {
                             return $d->id;
                         }
                ],
                [
                    'label' => 'Created By',
                    'value' => function ($d) {
                        if(is_object($d))
                            return $d->created->name;
                    },
                    /* 'filter' => Html::activeDropDownList($searchModel, 'created_by', \app\models\User::toArrayList(), ['prompt' => "Users", 'class' => 'form-control']),*/

                ],
                'created_at',
                [
                    'label' => 'Store',
                    'value' => function ($d) {
                        if(is_object($d))
                            return $d->store->name;
                        return ' - ';
                    },
                    //'filter' => Html::activeDropDownList($searchModel,'store_id',\app\models\Stores::toArrayList(),['prompt' => "Stores", 'class'=>'form-control']),
                ],
                'status',
                [
                    'label' => 'Issued To',
                    'value' => function ($d) {
                        if(is_object($d->user))
                            return $d->user->username;
                        return ' - ';
                    },
                    //'filter' => Html::activeDropDownList($searchModel, 'issuer', \app\models\User::toArrayList(), ['prompt' => "Users", 'class' => 'form-control']),

                ],
                'admin_incharge',
                'project_manager',
            ],
        ]) ?>
        <br>
      <?= GridView::widget([
         'dataProvider' => $dataProvider,
          /*'filterModel' => $searchModel,*/
       'columns' => [

        ['class' => 'yii\grid\CheckboxColumn'],
        'Meter Serial Number',
        'Issued To',
        'Store',         

 ],
]); ?>
        <form>
            <p>
                <a href="<?= URL::toRoute('ogpheader/viewsetpdf')?>" type="submit" class="btn btn-primary" name="issue_pdf">Set PDF</a>
                <br/>

            </p>
        </form>

结果视图:

The resulted view is

现在想要将我的GridViewDetailView发送到新控制器。对于

新控制器

 public function actionViewsetpdf($id)
{
    $model = $this->findModel($id);
    print_r($model);
    exit();

}

在这里,我只想看看模型中的值。但是当我点击Set PDF按钮时,我总是得到

  

错误请求(#400)   缺少必需参数:id

我坚持不懈,因为我不知道为什么它没有向我显示数据。

任何帮助都将受到高度赞赏。

1 个答案:

答案 0 :(得分:1)

错误请求错误意味着您没有传递名称为id

的GET参数

您的请求字符串生成应该类似于

<?= URL::toRoute(['ogpheader/viewsetpdf','id'=>$model->id])?>