How to pass a complex model to an ActionAsPDF?

8.2k views Asked by At

I am creating an MVC 4 application. I am using Rotativa to generate pdfs

they have a method called

public ActionAsPdf(string action, object routeValues);

I am having trouble passing in a complex object to the routeValues

i.e:

I have a viewModel

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

when I step through, in the Index action the name is null... how do I pass the complex model through?

2

There are 2 answers

1
Ilya Sulimanov On BEST ANSWER

Check this

return new ActionAsPdf("Index", fullname ) { FileName = "Test.pdf" };
0
Saboor Awan On
public ActionResult viewForPDFFile(int id)
{
    Data data = new Manager().GetData(id);
    return View(data); // this view content will show in PDF File
}

then call it simply

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