Export View as PDF in ASP.NET MVC

First, you have to install a package called RazorPDF for MVC through Manage Nuget Packages.

I have created a class called MarksCard.cs,

  1. public class MarksCard  
  2. {  
  3.    public int RollNo { getset; }  
  4.    public string Subject { getset; }  
  5.    public int FullMarks { getset; }  
  6.    public int Obtained { getset; }  
  7. }  
Now create a HomeController.cs

And add a namespace.
  1. using RazorPDF;  
There are two Action Methods.
  1. public ActionResult Index()  
  2. {  
  3.     var studentMarks = new List < MarksCard > ()  
  4.     {  
  5.         new MarksCard  
  6.         {  
  7.             RollNo = 101, Subject = "C#", FullMarks = 100, Obtained = 90  
  8.         },  
  9.         new MarksCard  
  10.         {  
  11.             RollNo = 102, Subject = "asp.net", FullMarks = 100, Obtained = 80  
  12.         },  
  13.         new MarksCard  
  14.         {  
  15.             RollNo = 103, Subject = "MVC", FullMarks = 100, Obtained = 100  
  16.         },  
  17.         new MarksCard  
  18.         {  
  19.             RollNo = 104, Subject = "SQL Server", FullMarks = 100, Obtained = 75  
  20.         },  
  21.     };  
  22.     return View("Index", studentMarks);  
  23. }  
  24. public ActionResult DownloadPdf()  
  25. {  
  26.     var studentMarks = new List < MarksCard > ()  
  27.     {  
  28.         new MarksCard  
  29.         {  
  30.             RollNo = 101, Subject = "C#", FullMarks = 100, Obtained = 90  
  31.         },  
  32.         new MarksCard  
  33.         {  
  34.             RollNo = 102, Subject = "asp.net", FullMarks = 100, Obtained = 80  
  35.         },  
  36.         new MarksCard  
  37.         {  
  38.             RollNo = 103, Subject = "MVC", FullMarks = 100, Obtained = 100  
  39.         },  
  40.         new MarksCard  
  41.         {  
  42.             RollNo = 104, Subject = "SQL Server", FullMarks = 100, Obtained = 75  
  43.         },  
  44.     };  
  45.     return new PdfResult(studentMarks, "Index");  
  46. }  
View
  1. @model IEnumerable  
  2. <RazorPDFDemo.Models.MarksCard>  
  3.   
  4. @{  
  5.   
  6.     Layout = null;  
  7. }  
  8.   
  9.     <!DOCTYPE html>  
  10.     <html>  
  11.         <body>  
  12.             <h3>Using RazorPDF</h3>  
  13.             <table border="2" width='500' bordercolor="Green">  
  14.                 <tr>  
  15.                     <td colspan="3" bgcolor="LightBlue" align="center" valign="top">Marks Sheet 2016</td>  
  16.                 </tr>  
  17.                 <tr>  
  18.                     <td>  
  19.                         @{  
  20.                             var rollNumber = Model.Select(z => z.RollNo).Take(1).ToArray();  
  21.                         }  
  22.                         Mayank Sharma  
  23.                         <br />  
  24.                         RollNo:@rollNumber[0]  
  25.   
  26.                     </td>  
  27.                     <td colspan="2" >  
  28.                         <img src="@Server.MapPath("~/Images/ron.jpg")" width="100" height="100"/>  
  29.                     </td>  
  30.                 </tr>  
  31.                 <tr>  
  32.                     <td bgcolor="lightblue">  
  33.                         @Html.DisplayNameFor(model => model.Subject)  
  34.                     </td>  
  35.                     <td bgcolor="lightblue">  
  36.                         @Html.DisplayNameFor(model => model.FullMarks)  
  37.                     </td>  
  38.                     <td bgcolor="lightblue">  
  39.                         @Html.DisplayNameFor(model => model.Obtained)  
  40.                     </td>  
  41.                 </tr>  
  42.                 @{  
  43.                     int total = 0;  
  44.                 }  
  45.                 @foreach (var item in Model)  
  46.                 {  
  47.   
  48.                     <tr>  
  49.                         <td>  
  50.                             @Html.DisplayFor(x => item.Subject)  
  51.                         </td>  
  52.                         <td>  
  53.                             @Html.DisplayFor(modelItem => item.FullMarks)  
  54.                         </td>  
  55.                         <td>  
  56.                             @Html.DisplayFor(modelItem => item.Obtained)  
  57.                         </td>  
  58.                     </tr>  
  59.                     total += item.Obtained;  
  60.                 }  
  61.   
  62.                 <tr>  
  63.                     <td></td>  
  64.                     <td>  
  65.                         <strong>  
  66.                             <font color="RED">Total</font>  
  67.                         </strong>  
  68.                     </td>  
  69.                     <td>@total  
  70.                     </td>  
  71.                 </tr>  
  72.             </table>  
  73.         </body>  
  74.     </html>  
  75.     <br />  
  76.     <br />  
  77.     <a href="@Url.Action("DownloadPdf","Home")"> Download PDF</a>  
Here is the output as PDF,