Search code examples
ajaxjsonmodel-view-controllerjscript

Returning a form using ajax/jquery that includes html within an input


So, I'm trying to return a hidden field (which is part of a form submission) which contains a string of html formatted text, there are reasons for this, the html is being rendered internally into pdf. I am currently posting the form back with ajax. The form serializes but when the form contains the html string, it returns a 500 error when trying to find the controller.

Code:

 $(function () {
            $('#preview').click(function (evt) {
                //prevent the browsers default function
                evt.preventDefault();
                var $form = $('#sform');
                $.ajax({
                    type: $form.prop('method'),
                    url: $form.prop('action'),
                    data: $form.serialize(),
                    dataType: "json",
                    traditional: true,
                    success: function (response) {
                        var newURL = window.location.protocol + "//" + window.location.host + "//" + response;
                        document.getElementById('myIframe').src = newURL;
                    }
                });
            });
        });

Solution

  • Probably you need to turn off request validation. You can mark up your controller method with

    [ValidateInput(false)]
    

    Oh, and I think that for later versions of MVC you'll also need

    <system.web>
        <httpRuntime requestValidationMode="2.0"/>
        ...
    </system.web>