How to pass a complex model to an ActionAsPDF?

user2206329 picture user2206329 · Dec 17, 2013 · Viewed 7.6k times · Source

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?

Answer

Ilya picture Ilya · Dec 17, 2013

Check this

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