Magento – Back-End “Save and Continue Edit” Button Works Partially

backendcustomgridmagento-1.9module

I created a custom module and the "Save and Continue" button on the back-end grid edit page works partially. Either it saves the values and doesn't stay on the same (edit) page or it stays on the same page but doesn't save the values
(this happens when I uncomment the "setLocation" line in __construct() function).
How can I fix the button to both save the values and remain on the same page? Please help.

Here is the save action in controller file:

public function saveAction()
{
    //if (!$postData) return $this->getResponse()->setBody('$postData is empty');
    if ($postData = $this->getRequest()->getPost()) {
        $model = Mage::getSingleton('cpstest_productcomment/cps');
        $model->setData($postData);

        try {
            $model->save();

            Mage::getSingleton('adminhtml/session')->addSuccess($this->__('The comment has been saved.'));

            if ($this->getRequest()->getParam('back', false)) {
                 $this->_redirect('*/*/edit', array(
                    'id' => $model->getId(),
                    '_current' => true
                ));
            } else {
                $this->_redirect('*/*/');
            }
            return;
        }
        catch (Mage_Core_Exception $e) {
            Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
        }
        catch (Exception $e) {
            Mage::getSingleton('adminhtml/session')->addError($this->__('An error occurred while saving this comment.'));
        }

        Mage::getSingleton('adminhtml/session')->setCpsData($postData);
        $this->_redirectReferer();
    }
}

Best Answer

Add the below code in your admin block constructor

$this->_formScripts[] = " function saveAndContinueEdit(){
    editForm.submit($('edit_form').action+'back/edit/');
}";
Related Topic