Click here to Skip to main content
15,885,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
XML
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

C#
public ActionResult PDF()
        {
            var viewModel = new StudentCollection();

            viewModel.Students = db.Students.ToList().Where(s => s.StudentID == 1);

            viewModel.Modules = db.Modules.ToList();     //.Where(s => s.Courses.StudentID ==1); This is where a join is needed

            //viewModel.Departments = db.Departments.ToList();

            //viewModel.Courses = db.Courses.ToList();

            viewModel.Attendances = db.Attendances.ToList();   //.Where(a => a.StudentID == 1);

            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.

HTML
 @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
Posted
Updated 1-Mar-15 7:46am
v2
Comments
[no name] 25-Feb-15 7:06am    
What is the error message?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900