What's the problem I'm trying to solve
I'm trying to create a PDF which will take in information from my database.
The packages I'm using is iTextSharp and RazorPD.
I want to get the grades and attendance (separate tables) for a student
What I tried
First of all the PDF does generate and some of the view models do work
I tried using linq to bring the information across from the controller to the view but because the controller only takes in 1 value "return new RazorPDF.PdfResult(viewModel, "PDF");" it won't work.
I tried creating a view model which would hold collections
public class StudentCollection
{
public IEnumerable<Student> Students { get; set; }
public IEnumerable<Department> Departments { get; set; }
public IEnumerable<Module> Modules { get; set; }
public IEnumerable<Course> Courses { get; set; }
public IEnumerable<Grade> Grades { get; set; }
public IEnumerable<Attendance> Attendances { get; set; }
}
Then on the controller page I used Lambda syntax and store it in the viewmodel student collection but because of this in a collection you cant do joins with Lambda. If I do it will bring up a error
public ActionResult PDF()
{
var viewModel = new StudentCollection();
viewModel.Students = db.Students.ToList().Where(s => s.StudentID == 1);
viewModel.Modules = db.Modules.ToList();
viewModel.Attendances = db.Attendances.ToList();
viewModel.Grades = db.Grades.ToList().Where(g => g.StudentID == 1);
return new RazorPDF.PdfResult(viewModel, "PDF");
}
I then called this information into the view as shown below
//This is just one example.
@foreach (var item in Model.Students)
{
<tr>
<td> Student Name </td>
<td>
@Html.DisplayFor(modelItem => item.FirstName)
@Html.DisplayFor(modelItem => item.Lastname)
</td>
</tr>
}
Thank you in advance would appreciate help or advice