First, you have to install a package called RazorPDF for MVC through Manage
Nuget Packages.
I have created a class called MarksCard.cs,
- public class MarksCard
- {
- public int RollNo { get; set; }
- public string Subject { get; set; }
- public int FullMarks { get; set; }
- public int Obtained { get; set; }
- }
Now create a HomeController.cs
And add a namespace.
There are two Action Methods.
- public ActionResult Index()
- {
- var studentMarks = new List < MarksCard > ()
- {
- new MarksCard
- {
- RollNo = 101, Subject = "C#", FullMarks = 100, Obtained = 90
- },
- new MarksCard
- {
- RollNo = 102, Subject = "asp.net", FullMarks = 100, Obtained = 80
- },
- new MarksCard
- {
- RollNo = 103, Subject = "MVC", FullMarks = 100, Obtained = 100
- },
- new MarksCard
- {
- RollNo = 104, Subject = "SQL Server", FullMarks = 100, Obtained = 75
- },
- };
- return View("Index", studentMarks);
- }
- public ActionResult DownloadPdf()
- {
- var studentMarks = new List < MarksCard > ()
- {
- new MarksCard
- {
- RollNo = 101, Subject = "C#", FullMarks = 100, Obtained = 90
- },
- new MarksCard
- {
- RollNo = 102, Subject = "asp.net", FullMarks = 100, Obtained = 80
- },
- new MarksCard
- {
- RollNo = 103, Subject = "MVC", FullMarks = 100, Obtained = 100
- },
- new MarksCard
- {
- RollNo = 104, Subject = "SQL Server", FullMarks = 100, Obtained = 75
- },
- };
- return new PdfResult(studentMarks, "Index");
- }
View
- @model IEnumerable
- <RazorPDFDemo.Models.MarksCard>
-
- @{
-
- Layout = null;
- }
-
- <!DOCTYPE html>
- <html>
- <body>
- <h3>Using RazorPDF</h3>
- <table border="2" width='500' bordercolor="Green">
- <tr>
- <td colspan="3" bgcolor="LightBlue" align="center" valign="top">Marks Sheet 2016</td>
- </tr>
- <tr>
- <td>
- @{
- var rollNumber = Model.Select(z => z.RollNo).Take(1).ToArray();
- }
- Mayank Sharma
- <br />
- RollNo:@rollNumber[0]
-
- </td>
- <td colspan="2" >
- <img src="@Server.MapPath("~/Images/ron.jpg")" width="100" height="100"/>
- </td>
- </tr>
- <tr>
- <td bgcolor="lightblue">
- @Html.DisplayNameFor(model => model.Subject)
- </td>
- <td bgcolor="lightblue">
- @Html.DisplayNameFor(model => model.FullMarks)
- </td>
- <td bgcolor="lightblue">
- @Html.DisplayNameFor(model => model.Obtained)
- </td>
- </tr>
- @{
- int total = 0;
- }
- @foreach (var item in Model)
- {
-
- <tr>
- <td>
- @Html.DisplayFor(x => item.Subject)
- </td>
- <td>
- @Html.DisplayFor(modelItem => item.FullMarks)
- </td>
- <td>
- @Html.DisplayFor(modelItem => item.Obtained)
- </td>
- </tr>
- total += item.Obtained;
- }
-
- <tr>
- <td></td>
- <td>
- <strong>
- <font color="RED">Total</font>
- </strong>
- </td>
- <td>@total
- </td>
- </tr>
- </table>
- </body>
- </html>
- <br />
- <br />
- <a href="@Url.Action("DownloadPdf","Home")"> Download PDF</a>
Here is the output as PDF,