【问题标题】:How to pass a complex model to an ActionAsPDF?如何将复杂模型传递给 ActionAsPDF?
【发布时间】:2016-11-05 03:35:57
【问题描述】:

我正在创建一个 MVC 4 应用程序。我正在使用 Rotativa 生成 pdf 文件

他们有一个方法叫

public ActionAsPdf(string action, object routeValues);

我在将复杂对象传递给 routeValues 时遇到问题

即:

我有一个视图模型

public class FullName
{
    public string FirstName { get; set; }
    public string Surname { get; set; }
}

public ActionResult Index(FullName name)
{
    ViewBag.Message = string.Format("Hello {0} to ASP.NET MVC!", name);
    return View();
}

public ActionResult Test()
{
    var fullname = new FullName();
    fullname.FirstName = "John";
    fullname.Surname = "Smith";

    return new ActionAsPdf("Index", new { name = fullname }) { FileName = "Test.pdf" };
}

当我单步执行时,在 Index 操作中名称为 null...如何通过复杂模型?

【问题讨论】:

    标签: asp.net-mvc-4 rotativa


    【解决方案1】:

    检查一下

    return new ActionAsPdf("Index", fullname ) { FileName = "Test.pdf" };
    

    【讨论】:

    • 如何将两个视图模型传递给 ActionAsPdf?
    【解决方案2】:
    public ActionResult viewForPDFFile(int id)
    {
        Data data = new Manager().GetData(id);
        return View(data); // this view content will show in PDF File
    }
    

    那就简单地称呼它

    return new ActionAsPdf("viewForPDFFile", new { id = id} ) { FileName = String.Format("File_{0}.pdf",id) };
    

    【讨论】:

      猜你喜欢
      • 2016-10-01
      • 2021-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-18
      • 1970-01-01
      • 2016-01-29
      • 2014-04-01
      相关资源
      最近更新 更多