简体   繁体   中英

How to pass data from a view to a controller - yii

I've created a form using the standard yii crud system that holds all of the "paylists" on a certain account. Within each of the paylists there are certain recipients. (ie john might be in the paylist for "workers in march", while suzy would be in the pay list "workers in may"). I am trying to create a button in _view.php that allows me to redirect to the recipient page and passes in the information of which list he choice (Which I call list_id).

I've been trying to do this all within the view with a link. This is in _view.php

<?php 
$list_id = $data->id;
echo CHtml::link('Manage',array('recipient/index', 'list_id'=>$list_id)); 
?> 

But, in my controller I am unable to call $list_id. Should I be doing this in a different way? Should I be trying to pass data from my view to my controller at all?

___________________ EDIT

The controller looks like this after the edits suggested

public function actionIndex($list_id)
    {
        // echo $_GET["id"];
        $dataProvider= new CActiveDataProvider('Recipient', array('criteria'=>array(
                                                'condition'=>'list_id = '.$list_id)));
        $this->render('index',array(
            'dataProvider'=>$dataProvider,
        ));
    }

Yii has added support for automatic action parameter binding. a controller action method can define named parameters whose value will be automatically populated from $_GET by Yii.

for example :

public function actionIndex($list_id)

You can use the example above or simply call by using $_GET['list_id']

Try this -

Pass value like this in the view file

echo CHtml::link('Manage',array("recipient/index/list_id/$list_id"));

and your index function in controller should be like this:-

public function actionindex($list_id = null)
{
   echo $list_id;
}

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