I have a bit of ajax that is working perfectly fine. It posts to a C# controller that returns a file. Unfortunately I think Ajax is unable to initialize the file download. I really need to post JSON. The JSON contains filters that are applied to a table by the user. I have tried posting the JSON as a string from a input but it doesn't work the same and never populates my object. I realize that my C# code below does nothing yet but return a file but I will be adding more to it after I can get the simple file download to work.
Javascript:
$.ajax(
data: JSON.stringify(model),
contentType: 'application/json',
type: 'POST',
url: 'Respondents/DownloadCSV',
success: function () { alert('success'); },
error: function () { alert('unsuccessful'); }
});
C#
[HttpPost]
public ActionResult DownloadCSV(RespondentViewModel model)
{
string csv = "Name, Source, Email, City, State, Gender, Ethnicity, Age, Last Edit, Owner, Group, Status Date, Status";
return File(new System.Text.UTF8Encoding().GetBytes(csv), "text/csv", "DispositionReport.csv");
}