简体   繁体   English

如何将数据从视图传递到控制器-Yii

[英]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. 我已经使用标准yii crud系统创建了一个表格,该表格将所有“付款清单”保存在某个帐户中。 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). 我正在尝试在_view.php中创建一个按钮,该按钮可让我重定向到收件人页面, 传递他选择的列表信息(我称为list_id)。

I've been trying to do this all within the view with a link. 我一直在尝试通过链接在视图内完成所有这些操作。 This is in _view.php 这是在_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. 但是,在我的控制器中,我无法调用$ 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. Yii增加了对自动操作参数绑定的支持。 a controller action method can define named parameters whose value will be automatically populated from $_GET by Yii. 控制器操作方法可以定义命名参数,其值将由Yii从$ _GET自动填充。

for example : 例如 :

public function actionIndex($list_id)

You can use the example above or simply call by using $_GET['list_id'] 您可以使用上面的示例,也可以使用$ _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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM