title: Magento 2 adminhtml multiselect and showing selected options after save
body: <p>How do I get the Magento2 Adminhtml Form with a Multiselect to select the saved options?</p>
  
  <p><a href="https://i.stack.imgur.com/AltPn.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/AltPn.png" alt="enter image description here"></a></p>
  
  <p>Here is my Adminhtml Form Class for reference.
      
  
  <pre><code>namespace RussellAlbin\Blog\Block\Adminhtml\Post\Edit;
  
  /**
   * Adminhtml blog post edit form
   */
  class Form extends \Magento\Backend\Block\Widget\Form\Generic
  {
  
      /**
       * @var \RussellAlbin\Blog\Model\Category\Source\ListCategories
       */
      protected $_categories;
  
      /**
       * @var \RussellAlbin\Blog\Model\Postcategory
       */
      protected $_postcategory;
  
      /**
       * @var \Magento\Store\Model\System\Store
       */
      protected $_systemStore;
  
  
      /**
       * @param \Magento\Backend\Block\Template\Context $context
       * @param \Magento\Framework\Registry $registry
       * @param \Magento\Framework\Data\FormFactory $formFactory
       * @param \Magento\Store\Model\System\Store $systemStore
       * @param \RussellAlbin\Blog\Model\Category\Source\ListCategories $categories
       * @param \RussellAlbin\Blog\Model\Postcategory $postcategory
       * @param array $data
       */
      public function __construct(
          \Magento\Backend\Block\Template\Context $context,
          \Magento\Framework\Registry $registry,
          \Magento\Framework\Data\FormFactory $formFactory,
          \Magento\Store\Model\System\Store $systemStore,
          \RussellAlbin\Blog\Model\Category\Source\ListCategories $categories,
          \RussellAlbin\Blog\Model\Postcategory $postcategory,
          array $data = []
      ) {
          $this-&gt;_categories = $categories;
          $this-&gt;_systemStore = $systemStore;
          $this-&gt;_postcategory = $postcategory;
          parent::__construct($context, $registry, $formFactory, $data);
      }
  
      /**
       * Init form
       *
       * @return void
       */
      protected function _construct()
      {
          parent::_construct();
          $this-&gt;setId('blog_post_form');
          $this-&gt;setTitle(__('Blog Post Information'));
      }
  
      /**
       * Prepare form
       *
       * @return $this
       */
      protected function _prepareForm()
      {
          /** @var \RussellAlbin\Blog\Model\Post $model */
          $model = $this-&gt;_coreRegistry-&gt;registry('blog_post');
  
          /** @var \Magento\Framework\Data\Form $form */
          $form = $this-&gt;_formFactory-&gt;create(
              ['data' =&gt; ['id' =&gt; 'edit_form', 'action' =&gt; $this-&gt;getData('action'), 'method' =&gt; 'post']]
          );
  
          $form-&gt;setHtmlIdPrefix('post_');
  
          $fieldset = $form-&gt;addFieldset(
              'base_fieldset',
              ['legend' =&gt; __('General Information'), 'class' =&gt; 'fieldset-wide']
          );
  
          if ($model-&gt;getPostId()) {
              $fieldset-&gt;addField('post_id', 'hidden', ['name' =&gt; 'post_id']);
          }
  
          // Gather our existing categories
          $currentCategories = $this-&gt;_getExistingCategories( $model );
  
          // Get all the categories that in the database
          $allCategories = $this-&gt;_categories-&gt;toOptionArray();
  
          $field = $fieldset-&gt;addField(
              'blog_categories',
              'multiselect',
              [
                  'label' =&gt; __('Categories'),
                  'required' =&gt; true,
                  'name' =&gt; 'blog_categories',
                  'values' =&gt; $allCategories,
                  'value' =&gt; $currentCategories
              ]
          );
  
  
          $form-&gt;setValues($model-&gt;getData());
          $form-&gt;setUseContainer(true);
          $this-&gt;setForm($form);
  
          return parent::_prepareForm();
      }
  
      /**
       * @param $model
       * @return array
       */
      private function _getExistingCategories( $model )
      {
          // Get our collection
          $existingCategories = $this-&gt;_postcategory-&gt;getCollection()
              -&gt;addFieldToSelect('category_id')
              -&gt;addFieldToFilter('post_id', $model-&gt;getId());
  
          // Setup our placeholder for the array of categories needed to set back on the value of the multiselect
          $itemList = array();
          foreach($existingCategories as $_item)
          {
              $itemList[] = $_item['category_id'];
          }
          return $itemList;
      }
   }
  </code></pre>
  
  <p>I looked back at some work I have done in Magento 1.x and I the only way I got this to work before was using javascript and the setAfterElementHtml to set the options that matched.  I remember hating this method of getting it accomplished because it seemed like a work around.</p>