1, Open Visual Studio(Microsoft Visual Studio Express 2013 for Web which I used in this case)
2, FILE -> New Project...
3, Settings
4, Create Model Sample
5, Add View under Home
6, TOOLS -> NuGet Package Manager -> Package Manager Console
Install Rotativa:
run: Install-Package Rotativa
7, Specified code:
HomeController.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RotativaSample.Models;
namespace RotativaSample.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";
return View();
}
public ActionResult About()
{
ViewBag.Message = "Your app description page.";
return View();
}
public ActionResult Contact()
{
ViewBag.Message = "Your contact page.";
return View();
}
public ActionResult GetSamples()
{
List< Sample> samples = new List< Sample>();
samples.Add( new Sample() { Name = "tmp1", Email = "[email protected]" });
samples.Add( new Sample() { Name = "tmp2", Email = "[email protected]" });
return View(samples);
}
public ActionResult GeneratePDF()
{
return new Rotativa. ActionAsPdf("GetSamples" );
}
}
}
Sample.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace RotativaSample.Models
{
public class Sample
{
public string Name { get; set; }
public string Email { get; set; }
}
}
GetSamples.cshtml:
@model IEnumerable<RotativaSample.Models. Sample>
<h1> GetSamples</h1 >
<table class="table">
<tr >
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.Email)
</th>
</tr >
@ foreach ( var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Email)
</td>
</tr>
}
</table>
<p>
<a href="GeneratePDF"> GeneratePDF</a >
8, Publish
9, TEST
10, Get error:
Access is denied
11, Upload dlls:
msvcp120.dll
msvcr120.dll
Rotativa need component of "Visual C++ Redistributable for Visual Studio". I use version of 2013, and I can find it under my local via:
C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\Packages\Debugger\X64\msvcp120.dll
C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\Packages\Debugger\X64\msvcr120.dll
You can refer above path on your local with your own specified path to find it.
Upload them to "Rotativa" folder.
12, Finished.
Article ID: 1702, Created: April 22, 2015 at 10:28 AM, Modified: April 23, 2015 at 3:20 AM