1

In Magento 2.3.3, I save form data by ajax and need to display success message after save records.

Expected Result :

Message manager should be display.

Actual Result :

Message manager not display. It will display after page reload.

Controller code :

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Vendor\Magento\Controller\Adminhtml\Post;

class Save extends \Magento\Backend\App\Action {

    /**
     * @var \Magento\Framework\View\Result\PageFactory
     */
    protected $resultPageFactory;

    /**
     * @var \Magento\Backend\Model\Session
     */
    protected $adminsession;

    /**
     * @param \Magento\Backend\App\Action\Context        $context           [description]
     * @param \Magento\Framework\View\Result\PageFactory $resultPageFactory [description]
     */
    public function __construct(
        \Magento\Backend\App\Action\Context $context,
        \Magento\Framework\View\Result\PageFactory $resultPageFactory,
        \Magento\Backend\Model\Session $adminsession
    ) {
        parent::__construct($context);
        $this->resultPageFactory = $resultPageFactory;
        $this->adminsession = $adminsession;
    }

    public function execute() {
        $resultRedirect = $this->resultRedirectFactory->create();
        /** Save Logic */
        try {
                $object->save();
                $this->messageManager->addSuccessMessage(__('Data has been saved.'));
                //$this->messageManager->addMessages($this->messageManager->getMessages(true));
                //$this->messageManager->setMessages($this->messageManager->getMessages(true));
                $this->adminsession->setFormData(false);
            } catch (\Magento\Framework\Exception\LocalizedException $e) {
                $this->messageManager->addError($e->getMessage());
            } catch (\RuntimeException $e) {
                $this->messageManager->addError($e->getMessage());
            } catch (\Exception $e) {
                $this->messageManager->addException($e, __('Something went wrong while saving the data.'));
            }
            $this->_getSession()->setFormData($postData);
    }
}

Note : I don't want to set message using jQuery.

Any help would be appreciated.

Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.