title: How to save data to custom table using custom model in magento2
body: <p>I have a custom table and I have written a custom model for it too, but I am not able to understand how can I perform something like <code>Mage::getModel('')-&gt;setData()</code>. I have followed the required structure, created the Model class and specified the resource model and defined the collection class, I can retrieve the data in the admin grid. But, still I am not able to understand how can I make use of my model to <code>setData()</code> and <code>getData()</code>. Here is my controller, to which I am making an AJAX call and I want to save my data to my custom table.</p>
  
  <p><code>Rent.php</code></p>
  
  <pre><code>&lt;?php
  
  namespace Mofosys\Fastcure\Controller\Index;
  
  use Magento\Framework\Controller\ResultFactory;
  use Magento\Framework\App\Action\Action;
  use Magento\Framework\App\Action\Context;
  use Vendor\Module\Model\ModuleFactory;
  
  class Rent extends Action {
  
      protected $request;
      protected $_moduleFactory;
  
      public function __construct(Context $context, moduleFactory $moduleFactory) {
          $this-&gt;_moduleFactory = $moduleFactory;
          parent::__construct($context);
      }
  
      public function execute() {
          $model = $this-&gt;_moduleFactory-&gt;create();
          $data = $this-&gt;getRequest()-&gt;getPost();
          $model-&gt;setName($data['name']);
          $resultJson = $this-&gt;resultFactory-&gt;create(ResultFactory::TYPE_JSON);
          $resultJson-&gt;setData($data['name']);
          return $resultJson;
      }
  }
  </code></pre>
  
  <p>My models are in the following structure:</p>
  
  <p><code>|-Model
     |-ResourceModel
     |    |-Module
     |    |    |-Collection.php
     |    |-Module.php
     |-Module.php</code></p>
  
  <p>Now, I am not able to understand that how can I insert this data in the table from my controller using my custom model.</p>
  
  <p><code>Module/Model/Module.php</code></p>
  
  <pre><code>&lt;?php
  namespace Vendor\Module\Model;
  
  use Magento\Framework\Exception\LocalizedException as CoreException;
  
  class Fastcure extends \Magento\Framework\Model\AbstractModel {
  
      public function _construct() {
          $this-&gt;_init('Vendor\Module\Model\ResourceModel\Module');
      }
  
      public function getName() {
          return $this-&gt;getData(self::name);
      }
  
      public function setName($name) {
          return $this-&gt;setData(self::name, $name);
      }
  }
  </code></pre>
  
  <p>All of this, doesn't seem to work, please help me out here guys.</p>