draken
draken

Reputation: 31

C# Rotativa ActionAsPDF no parameters passed

I'm trying to send parameters with ActionAsPdf using the Rotativa Library (1.6.4), unfortunately, the function is called but the parameter trainee in it is always null. Here's my code:

List<T_Trainee> trainee= new List<T_Trainee>();
foreach (int f in foo)
{
    T_Trainee t = new T_Trainee();
    t.email = (string)Session["Mail"];
    t.phone = (string)Session["Phone"];
    trainee.Add(t);
}

//code to get the PDF     
ActionAsPdf pdf = new ActionAsPdf("Index", trainee) { FileName = "Bulletin.pdf" };

Trainee var is a list of object T_Trainee not null -> seen in debug:

//function that return the PDF
public ActionResult Index(List<T_Trainee> trainee)
{
    ViewModelFoo vmc = new ViewModelFoo();
    vmc.trainee = trainee;
    return View(vmc);
}

When the function is call in debug mode, I can clearly see that the parameter "trainee" is null but I still don't understand why.

Can anyone help me? Thanks!

Upvotes: 1

Views: 5693

Answers (3)

Stephen Zeng
Stephen Zeng

Reputation: 2818

The second parameter of ActionAsPdf() is type of RouteValueDictionary, which is a dictionary of key and value. You passed in a custom type thereby it converts it to null. It should work if you pass a RouteValueDictionary instead.

ViewAsPdf() receives an object parameter and treats it as a model for view binding that's why it works.

You can have a look at its source code here: https://github.com/webgio/Rotativa/blob/master/Rotativa/ActionAsPdf.cs

Upvotes: 0

draken
draken

Reputation: 31

ActionAsPdf seems to be a deprecated function in the last version of Rotativa.

I changed it by ViewAsPdf and now it works. The difference between the two function is that you have to send directly the view model inside the Index method call with ViewAsPdf.

Here's my code, I hope that it will help someone :

Code to call the index and send the viewModel

ViewModelFoo vmc = new ViewModelFoo();
List<T_Trainee> trainees= new List<T_Trainee>();
foreach (int f in foo)
{
    T_Trainee t = new T_Trainee();
    t.email = (string)Session["Mail"];
    t.phone = (string)Session["Phone"];
    trainees.Add(t);
}
vmc.trainees = trainees;
//code to get the PDF     
ViewAsPdf pdf = new ViewAsPdf("Index", vmc)
{
   FileName = "File.pdf",
   PageSize = Rotativa.Options.Size.A4,
   PageMargins = { Left = 0, Right = 0 }
};

Index that generate the view

public ActionResult Index()
{
    return View(vmc);
}

Upvotes: 2

Michael Goldshmidt
Michael Goldshmidt

Reputation: 141

Is foo populated?

Try sample code...

List trainee= new List(); trainee.Add(new T_Trainee {email = "[email protected]", phone = "555-1212"});

Does that work?

You can also try to bind Action to a model

public ActionResult Index(ViewModelFoo vmc)

Upvotes: 0

Related Questions