I have a json result in my view,which I need to send it back to controller,to convert it to .CSV file,but I get null in controller even thought its contain data,here is my view the "result" contains data but in my controller I get it null,any help will be highly appreciated
$.ajax({
dataType: "json",
type: "POST",
url: "@Url.Action("CreateCSVFile", "Turbine")",
contentType: "application/json; charset=utf-8",
data: JSON.stringify({ "result": result }),
success: function (result) {
}
})
and here is my controller which I get NULL:
public void CreateCSVFile(string result)
{
//XmlNode xml = JSON JSON.DeserializeXmlNode("{records:{record:" + json + "}}");
DataTable dt = JsonConvert.DeserializeObject<DataTable>(result);
string strFilePath = @"C:\myCSVfile.csv";
// Create the CSV file to which grid data will be exported.
StreamWriter sw = new StreamWriter(strFilePath, false);
//First we will write the headers.
int iColCount = dt.Columns.Count;
for (int i = 0; i < iColCount; i++)
{
sw.Write(dt.Columns[i]);
if (i < iColCount - 1)
{
sw.Write(",");
}
}
sw.Write(sw.NewLine);
// Now write all the rows.
foreach (DataRow dr in dt.Rows)
{
for (int i = 0; i < iColCount; i++)
{
if (!Convert.IsDBNull(dr[i]))
{
sw.Write(dr[i].ToString());
}
if (i < iColCount - 1)
{
sw.Write(",");
}
}
sw.Write(sw.NewLine);
}
sw.Close();
}