Php 无法将模型id传递给视图

Php 无法将模型id传递给视图,php,model-view-controller,yii2,yii2-advanced-app,Php,Model View Controller,Yii2,Yii2 Advanced App,我正在使用yii2。我想将模型id传递给视图,但在传递过程中出现错误 控制器 public function actionProcess($id){ try { $model = $this->findModel($id); } catch (NotFoundHttpException $e) { } // this will find my model/record based on the id $file_name = $_GET['f

我正在使用
yii2
。我想将模型id传递给视图,但在传递过程中出现错误

控制器

public function actionProcess($id){
    try {
        $model = $this->findModel($id);
    } catch (NotFoundHttpException $e) {
    } // this will find my model/record based on the id
    $file_name = $_GET['file_name'];

    // $data = \moonland\phpexcel\Excel::import("uploads/test.xlsx"); // $config is an optional

    try {
        $header_index = $_GET['header_no'];

        $data = \moonland\phpexcel\Excel::widget([
            'mode' => 'import',
            'fileName' => 'uploads/' . $file_name,
            'setFirstRecordAsKeys' => false, // if you want to set the keys of record column with first record, if it not set, the header with use the alphabet column on excel.
            'setIndexSheetByName' => false, // set this if your excel data with multiple worksheet, the index of array will be set with the sheet name. If this not set, the index will use numeric.
            'getOnlySheet' => 0, // you can set this property if you want to get the specified sheet from the excel data with multiple worksheet.
        ]);
        if (isset($data[0])) {
            $headers = $data[0][$header_index];
        } else {
            $headers = $data[$header_index];
        }

    }catch (Exception $x){
        print_r($x->errorInfo);
    }

    return $this->render('excel_options',['headers'=>$headers,'file_name'=>$file_name,'header_index'=>$header_index,'id'=>$model->id]);

}
查看

<div class="box">

    <!-- /.box-header -->
    <div class="box-body">

        <form action="import" method="post">
     <input type="hidden" name="file_name" value="<?=$_GET['file_name']?>" />
            <input type="hidden" name="header_index" value="<?= $_GET['header_no'] ?>"/>
            <h1>Maping</h1>

            <div class="row">
                <div class="col-md-2">
                  Ref #:
                </div>
                <div class="col-md-4">
                 <select name="field[0][ref_no]" class="form-control">
                    <option value="">Select A field</option>
                <?php foreach($headers as $k=>$v) { ?>
                     <?php if (trim($v) != '') { ?>
                    <option value="<?=$k?>"><?=$v?></option>
                         <?php } ?>
                <?php } ?>
                </select>
                    </div>
            </div>

            <div class="row">
                <div class="col-md-2">
                Meter MSN:
                    </div>
                <div class="col-md-4">
                <select name="field[0][meter_msn]" class="form-control">
                    <option value="">Select A field</option>
                    <?php foreach ($headers as $k => $v) { ?>
                    <?php if (trim($v) != '') { ?>
                        <option value="<?= $k ?>"><?= $v ?></option>
                        <?php } ?>
                    <?php } ?>
                </select>
                    </div>
            </div>

            <div class="row">
                <div class="col-md-2">
                    Meter Type:
                </div>
                <div class="col-md-4">
                    <select name="field[0][meter_type]" class="form-control">
                        <option value="">Select A field</option>
                        <?php foreach ($headers as $k => $v) { ?>
                            <?php if (trim($v) != '') { ?>
                                <option value="<?= $k ?>"><?= $v ?></option>
                            <?php } ?>
                        <?php } ?>
                    </select>
                </div>
            </div>

            <div class="row">
                <div class="col-md-2">
                   Sub-Div:
                </div>
                <div class="col-md-4">
                    <select name="field[0][sub_div]" class="form-control">
                        <option value="">Select A field</option>
                        <?php foreach ($headers as $k => $v) { ?>
                            <?php if (trim($v) != '') { ?>
                                <option value="<?= $k ?>"><?= $v ?></option>
                            <?php } ?>
                        <?php } ?>
                    </select>
                </div>
            </div>


            <div class="row">
                <div class="col-md-2"></div>

                <div class="col-md-4">
                    <br />
                    <a href="<?= URL::toRoute(['meteracceptanceheader/import', 'id'=>$model->id])?>" name="redirect" class="btn btn-primary" id="myid">Submit</a>
                </div>
            </div>
        </form>

    </div>
</div>
在我看来,我试图在提交按钮中传递模型id

 <div class="row">
                <div class="col-md-2"></div>

                <div class="col-md-4">
                    <br />
                    <a href="<?= URL::toRoute(['meteracceptanceheader/import', 'id'=>$model->id])?>" name="redirect" class="btn btn-primary" id="myid">Submit</a>
                </div>
            </div>
更新1

下面是我从中呈现
excel\u选项的控制器

public function actionProcess($id){

        $model = $this->findModel($id);
     // this will find my model/record based on the id
    $file_name = $_GET['file_name'];

    // $data = \moonland\phpexcel\Excel::import("uploads/test.xlsx"); // $config is an optional

    try {
        $header_index = $_GET['header_no'];

        $data = \moonland\phpexcel\Excel::widget([
            'mode' => 'import',
            'fileName' => 'uploads/' . $file_name,
            'setFirstRecordAsKeys' => false, // if you want to set the keys of record column with first record, if it not set, the header with use the alphabet column on excel.
            'setIndexSheetByName' => false, // set this if your excel data with multiple worksheet, the index of array will be set with the sheet name. If this not set, the index will use numeric.
            'getOnlySheet' => 0, // you can set this property if you want to get the specified sheet from the excel data with multiple worksheet.
        ]);
        if (isset($data[0])) {
            $headers = $data[0][$header_index];
        } else {
            $headers = $data[$header_index];
        }

    }catch (Exception $x){
        print_r($x->errorInfo);
    }

    return $this->render('excel_options',['headers'=>$headers,'file_name'=>$file_name,'header_index'=>$header_index,'id'=>$model->id]);

}
查看

<div class="box">

    <!-- /.box-header -->
    <div class="box-body">

        <form action="import" method="post">
     <input type="hidden" name="file_name" value="<?=$_GET['file_name']?>" />
            <input type="hidden" name="header_index" value="<?= $_GET['header_no'] ?>"/>
            <h1>Maping</h1>

            <div class="row">
                <div class="col-md-2">
                  Ref #:
                </div>
                <div class="col-md-4">
                 <select name="field[0][ref_no]" class="form-control">
                    <option value="">Select A field</option>
                <?php foreach($headers as $k=>$v) { ?>
                     <?php if (trim($v) != '') { ?>
                    <option value="<?=$k?>"><?=$v?></option>
                         <?php } ?>
                <?php } ?>
                </select>
                    </div>
            </div>

            <div class="row">
                <div class="col-md-2">
                Meter MSN:
                    </div>
                <div class="col-md-4">
                <select name="field[0][meter_msn]" class="form-control">
                    <option value="">Select A field</option>
                    <?php foreach ($headers as $k => $v) { ?>
                    <?php if (trim($v) != '') { ?>
                        <option value="<?= $k ?>"><?= $v ?></option>
                        <?php } ?>
                    <?php } ?>
                </select>
                    </div>
            </div>

            <div class="row">
                <div class="col-md-2">
                    Meter Type:
                </div>
                <div class="col-md-4">
                    <select name="field[0][meter_type]" class="form-control">
                        <option value="">Select A field</option>
                        <?php foreach ($headers as $k => $v) { ?>
                            <?php if (trim($v) != '') { ?>
                                <option value="<?= $k ?>"><?= $v ?></option>
                            <?php } ?>
                        <?php } ?>
                    </select>
                </div>
            </div>

            <div class="row">
                <div class="col-md-2">
                   Sub-Div:
                </div>
                <div class="col-md-4">
                    <select name="field[0][sub_div]" class="form-control">
                        <option value="">Select A field</option>
                        <?php foreach ($headers as $k => $v) { ?>
                            <?php if (trim($v) != '') { ?>
                                <option value="<?= $k ?>"><?= $v ?></option>
                            <?php } ?>
                        <?php } ?>
                    </select>
                </div>
            </div>


            <div class="row">
                <div class="col-md-2"></div>

                <div class="col-md-4">
                    <br />
                    <a href="<?= URL::toRoute(['meteracceptanceheader/import', 'id'=>$model->id])?>" name="redirect" class="btn btn-primary" id="myid">Submit</a>
                </div>
            </div>
        </form>

    </div>
</div>


尝试传递的不是id,而是$model本身

return $this->render('excel_options',['headers'=>$headers,'file_name'=>$file_name,'header_index'=>$header_index,'model'=>$model]);
并将代码保存在视图中

 <a href="<?= URL::toRoute(['meteracceptanceheader/import', 'id'=>$model->id])?>" name="redirect" class="btn btn-primary" id="myid">Submit</a>

尝试传递的不是id,而是$model本身

return $this->render('excel_options',['headers'=>$headers,'file_name'=>$file_name,'header_index'=>$header_index,'model'=>$model]);
并将代码保存在视图中

 <a href="<?= URL::toRoute(['meteracceptanceheader/import', 'id'=>$model->id])?>" name="redirect" class="btn btn-primary" id="myid">Submit</a>


它给我
未定义的变量$id
它给我
未定义的变量$id
你能给我看一下查看文件吗?我给你的答案应该对你有帮助。也许你在调用另一个视图?@ZiyaVakhobov检查我的更新1我更改了我的答案请检查它你能给我显示视图文件吗?我给你的答案应该对你有帮助。也许你在调用另一个视图?@ZiyaVakhobov检查我的更新1我更改了我的答案请检查