I have a (ViewResult) Controller which receives a string parameter, generates a PDF file, and sends the PDF file back to the browser. I’ve tested the controller itself, and it works fine. Unfortunately, when I try to post to this controller from the $.ajax jQuery function (passing a simple string), the Controller always receives the string parameter as null. I've tried a hundred different configurations of the $.ajax function. Here's the controller, which returns a PDF to the browser (it works...as long as I create the HTML within the method):
[HttpPost]
public ActionResult HtmlToPdf(String htmlData)
{ }
Here's the jQuery I'm using in my view (triggered by a button click):
function getPdf() {
var htmlData = “blah, blah, etc.”;
$.ajax({
url: '/Home/HtmlToPdf',
type: 'post',
data: JSON.stringify(htmlData),
contentType: 'application/json; charset=utf-8',
success: handleSuccess,
error: handleError
});
}
I've tried 'post', 'get', json, text, html, stringify, different content types, etc. Does anyone know how to correctly send a string (the var 'htmlData' above) to a controller? Post? Get? Something else? Thanks.
data: htmlDatacontentTypeportion, I've found that even when encoding it as you have done (even with the correct type), by removing it, MVC will decipher it correctly. Just my 2 cents ;)