我正在使用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]);
}
查看
我认为我正在尝试在“提交”按钮中传递模型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>
现在,当我尝试访问excel_options
视图时,出现以下错误。
未定义变量:模型 在E:\ xampp \ htdocs \ inventory-web \ backend \ views \ meteracceptanceheader \ excel_options.php中的第103行
<a href="<?= URL::toRoute(['meteracceptanceheader/import', 'id'=>$model->id])?>" name="redirect" class="btn btn-primary" id="myid">Submit</a>
更新1
用于渲染excel_options
的控制器在
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传递到视图中?
任何帮助将不胜感激
答案 0 :(得分:2)
尝试不传递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>