[RESOLVED]How to return error messages when POSTing a form?

I’m offering a file for download – when the user clicks the "Export" button, the following Javascript is run:

function export() {
	var form = document.createElement('form');
	form.action = rootPath + 'MyView/ExportData';
	form.method = 'POST';

	document.body.appendChild(form);

	var formData = document.createElement('input');
	formData.name = 'inputParameters';
	formData.type = 'hidden';
	formData.value = JSON.stringify(input);

	form.appendChild(formData);

	form.submit();

	document.body.removeChild(form);
}

If everything is good the controller then returns a File(), which the user is prompted to download. The problem is if the input is somehow invalid and the user gets around client-side validation – is there any way to gracefully return an error message? Unlike
with AJAX requests, setting Response.StatusCode to 500 causes the browser to redirect to a "The website cannot display the page" error page.

you have done a form post. the controller can just return a view with an error message, and the browser will display it. if you want a more ajax type solution, then create a hidden iframe, and make this the target for the form. you can even create the form
in the iframe. in the case of error, the response can render javascript that calls the parent and displays an error.

 

Leave a Reply