返回PDF使用JSON和MVC浏览器? [英] Return PDF to browser using JSON and MVC?

查看:182
本文介绍了返回PDF使用JSON和MVC浏览器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个链接如下。

@Html.ActionLink("Create Report", "Screenreport", "Reports", null, new { @class = "subNavA AddBorderTop", id = "screenReport", title = "Create Report" })

一旦链接被点击,我有以下的jQuery code它创建一个JSON对象和发布信息。

Once the link is clicked, I have a the following jQuery code which creates a JSON object and post 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"); }
   }); 

};

要处理JSON我有以下两类。实现了在同一个命名空间两班

To Handle JSON I have the following two classes. Realize 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; } 
   }
}

和在我的控制器,我有以下的code:

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");

   }

这code应该GERATE PDF。
如果我离开[HttpPost],因为它是,它不产生PDF和它去/ Screenreport页,但是我看到我的JSON传递给控制器​​正常。
(screendata正确填充 - 在控制器)

This code is supposed to gerate PDF. if I leave [HttpPost] as it is, it does NOT generate PDF and it goes to /Screenreport page, however I see my JSON is passed to the controller properly. (screendata is populated properly - in controller)

但是,如果我注释掉[HttpPost],它生成的PDF,但screendata(在控制器)为null。

But if I comment out [HttpPost], it DOES generate a PDF but screendata (in controller) is null.

能否有人请解释什么是怎么回事,并帮助我找到答案。提前致谢。

Can someone please explain whats's going on and help me figure it out. Thanksin advance.

推荐答案

我觉得有义务张贴我的答案,因为我没有任何人听到。我结束了创建包含隐藏输入的表单,然后救了我的JSON对象中隐藏的输入,然后提交表单。这一次,我会得到输入作为字符串不是一个JSON或XML。

I feel obligated to post my answer since I didn't hear from anyone. I ended up creating a form that includes a hidden input, then saved my json object in the hidden input and then submit the form. This time I will get input as an string not a json or xml.

var $hidInput = $("#dataToReport"); 
$hidInput.val(JSON.stringify(Screentable)); 
$('#frmScreenreport').submit(); 

感谢所有反正。

这篇关于返回PDF使用JSON和MVC浏览器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆