Close Model without redirect

Good Afternoon,

I have a model that Pops up on click of a button, when saved it redirects. I would like to just close the model without redirecting is this possible?

Controller:

    public function actionAdd() { $model = new parts(); 
            $items = arrayhelper::map(partsgroups::find()->orderby('partgroup')->all(), 'ID','name');
            


        if ($model->load(Yii::$app->request->post()) && $model->save()) {
        //echo 1;

            } else {
        return $this->renderAjax('_form_popup', [
            'model' => $model,
            'items' => $items,
        ]);
    };
    }

JS:

    function(){
        //get the click of modal button to create / update item
        //we get the button by class not by ID because you can only have one id on a page and you can
        //have multiple classes therefore you can have multiple open modal buttons on a page all with or without
        //the same link.
    //we use on so the dom element can be called again if they are nested, otherwise when we load the content once it kills the dom element and wont let you load anther modal on click without a page refresh
          $(document).on('click', '.showModalButton', function(){
             //check if the modal is open. if it's open just reload content not whole modal
            //also this allows you to nest buttons inside of modals to reload the content it is in
            //the if else are intentionally separated instead of put into a function to get the 
            //button since it is using a class not an #id so there are many of them and we need
            //to ensure we get the right button and content. 
            if ($('#modal').data('bs.modal').isShown) {
                $('#modal').find('#modalContent')
                        .load($(this).attr('value'));
                //dynamiclly set the header for the modal
                document.getElementById('modalHeader').innerHTML = '<h4>' + $(this).attr('title') + '</h4>';
            } else {
                //if modal isn't open; open it and load content
                $('#modal').modal('show')
                        .find('#modalContent')
                        .load($(this).attr('value'));
                 //dynamiclly set the header for the modal
                document.getElementById('modalHeader').innerHTML = '<h4>' + $(this).attr('title') + '</h4>';
            }
        });
    });

it is possible, simply prevent default in your handler which will stop the execution of the handler

$(document).on('click', '.showModalButton', function(event) {
    // prevent default action to take place 
	event.preventDefault();

	if ($('#modal').data('bs.modal').isShown) {
		$('#modal').find('#modalContent')
			.load($(this).attr('value'));

		//dynamiclly set the header for the modal
		document.getElementById('modalHeader').innerHTML = '<h4>' + $(this).attr('title') + '</h4>';
		
		// close the modal 
		$("#modal").modal('close');
    } else {
    	// ...
    }
});

Perfect.

If i do that do i then need to write a function to save the model to the DB?

ok so didn’t work.

This is the controller:

public function actionAdd() { $model = new parts(); 
        $items = arrayhelper::map(partsgroups::find()->orderby('partgroup')->all(), 'ID','name');
        


    if ($model->load(Yii::$app->request->post()) && $model->save()) {
    //echo 1;

        } else {
    return $this->renderAjax('_form_popup', [
        'model' => $model,
        'items' => $items,
    ]);
};
}