I have a link as follows.
@Html.ActionLink("Create Report", "Screenreport", "Reports", null, new { @class = "subNavA AddBorderTop", id = "screenReport", title = "Create Report" })
After clicking the link, I have the following jQuery code that creates a JSON object and publishes the information.
$().ready(function () { // Create Report fron the screen data $("#screenReport").live("click", function (event) { GenerateScreenReport(this, event); }); }) /* end document.ready() */ function GenerateScreenReport(clikedtag, event) { var table = $(".EvrakTable").html(); var screendata = tableParser(table); var Screentable = { Screenlist: screendata }; var myurl = $(clikedtag).attr("href"); var title = $(clikedtag).attr("title"); $.ajax({ url: myurl, type: 'POST', data: JSON.stringify(Screentable), dataType: 'json', contentType: 'application/json', success: function () { alert("Got it"); } }); };
To handle JSON, I have the following two classes. Implement two classes in the same namespace
namespace MyProject.ViewModels { public class Screenrecord { public string Fname{ get; set; } public string LName { get; set; } public string Age { get; set; } public string DOB { get; set; } } public class Screentable { public List<Screenrecord> Screenlist { get; set; } } }
ANd in my controller, I have the following code:
[HttpPost] public FileStreamResult Screenreport(Screentable screendata) { MemoryStream outputStream = new MemoryStream(); MemoryStream workStream = new MemoryStream(); Document document = new Document(); PdfWriter.GetInstance(document, workStream); document.Open(); document.Add(new Paragraph("Hello World")); document.Add(new Paragraph(DateTime.Now.ToString())); document.Close(); byte[] byteInfo = workStream.ToArray(); outputStream.Write(byteInfo, 0, byteInfo.Length); outputStream.Position = 0; return new FileStreamResult(outputStream, "application/pdf"); }
This code should transmit PDF. if I leave [HttpPost] as it is, it will NOT generate a PDF and it will go to the / Screenreport page, however, I can see that my JSON is correctly transferred to the controller. (screendata fills correctly - in the controller)
But if I comment on [HttpPost], it generates a PDF, but screendata (in the controller) is null.
Can someone explain what is happening and help me figure it out. Thank you in advance.