ajaxSubmitButton:Yii 中控制器操作未重定向
我正在使用 ajaxSubmitButton 将数据发布到我的控制器。然后,该控制器操作在事务中生成两个插入语句。一切都运转良好。我现在想要的是一旦事务完成就重定向到新视图,但我无法让它工作。
当我单击按钮时,交易已处理,但我仍停留在同一页面上。我还尝试在按钮内使用“更新”并包装页面的一部分,但没有任何内容更新。但这并不是我最终想要的,因为我想最终渲染一个全新的视图。
这是我所拥有的...
查看
echo CHtml::ajaxSubmitButton('submit',
array('/player/mark'),
array(
'type'=>'POST',
'data' => array(...),
));
控制器
public function actionMark()
{
$connection = yii::app()->db;
$transaction=$connection->beginTransaction();
try
{
$connection = yii::app()->db;
$sql1 = "INSERT ...";
$command=$connection->createCommand($sql1);
...
$command->execute();
$connection = yii::app()->db;
$sql2 = "INSERT ...";
$command=$connection->createCommand($sql2);
...
$command->execute();
$transaction->commit();
$this->redirect(array('manage')); // THIS IS NOT WORKING
}
catch(Exception $e)
{
$transaction->rollBack();
$this->refresh;
}
}
I am using ajaxSubmitButton to POST data to my controller. That controller action then makes two insert statements in a transaction. All that is working just fine. What I want now is once the transaction is complete to be redirected to a new view but I can't get that to work.
When I click my button the transaction is processed but I remain on the same page. I have also tried using 'update' within the button and wrapping part of the page but none of the content updates. That is not ultimately what I am after though since I want to end up rendering a whole new view.
Here is what I have...
VIEW
echo CHtml::ajaxSubmitButton('submit',
array('/player/mark'),
array(
'type'=>'POST',
'data' => array(...),
));
CONTROLLER
public function actionMark()
{
$connection = yii::app()->db;
$transaction=$connection->beginTransaction();
try
{
$connection = yii::app()->db;
$sql1 = "INSERT ...";
$command=$connection->createCommand($sql1);
...
$command->execute();
$connection = yii::app()->db;
$sql2 = "INSERT ...";
$command=$connection->createCommand($sql2);
...
$command->execute();
$transaction->commit();
$this->redirect(array('manage')); // THIS IS NOT WORKING
}
catch(Exception $e)
{
$transaction->rollBack();
$this->refresh;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
重定向不起作用,因为您使用了 ajax。
尝试以下代码:
将 *your_url* 替换为您想要重定向到的网址
Redirect not working because you using ajax.
Try this code:
Replace *your_url* to url you want redirect to
运行成功后应添加window.location js。
参考这个ajax提交按钮示例:
更多Yii 示例
You should add window.location js after run success.
Reference to this ajax submition button example:
More Yii examples