How to: Implement Server-side Export in the ASP.NET MVC Dashboard Extension
- 5 minutes to read
This example demonstrates how to export a dashboard displayed using the ASP.NET MVC Dashboard extension on the server side using the WebDashboardExporter class. The following API is used to implement this capability:
- The ASPxClientDashboard.BeforeRender event is handled to obtain the client-side DashboardControl using the ASPxClientDashboard.GetDashboardControl method.
- The AJAX request is used to send the dashboard identifier and state to the server side. On the server side, these values are received as action method parameters and passed to the WebDashboardExporter.ExportToPdf method.
- _rootLayout.cshtml (C#)
- HomeController.cs
- DashboardExport.js (C#)
- DashboardConfig.cs
- Index.cshtml (C#)
- Global.asax.cs
- HomeController.vb
- DashboardConfig.vb
- Global.asax.vb
- Index.cshtml (VB.NET)
- _rootLayout.cshtml (VB.NET)
- DashboardExport.js (VB.NET)
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Dashboard Web Application</title>
<script src="@Url.Content("~/Scripts/DashboardExport.js")" type="text/javascript"></script>
@Html.DevExpress().GetStyleSheets(
new StyleSheet { ExtensionSuite = ExtensionSuite.Dashboard }
)
@Html.DevExpress().GetScripts(
new Script { ExtensionSuite = ExtensionSuite.Dashboard }
)
<link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
</head>
<body>
@RenderBody()
</body>
</html>
using DevExpress.DashboardCommon;
using DevExpress.DashboardWeb;
using System;
using System.IO;
using System.Web.Mvc;
namespace MvcDashboard_ServerExport.Controllers {
public class HomeController : Controller {
public ActionResult Index() {
return View();
}
[HttpPost]
public ActionResult ExportDashboardToPdf(string DashboardID, string DashboardState) {
using (MemoryStream stream = new MemoryStream()) {
string dashboardID = DashboardID;
DashboardState dashboardState = new DashboardState();
dashboardState.LoadFromJson(DashboardState);
DashboardPdfExportOptions pdfOptions = new DashboardPdfExportOptions();
pdfOptions.ExportFilters = true;
pdfOptions.DashboardStatePosition = DashboardStateExportPosition.Below;
string dateTimeNow = DateTime.Now.ToString("yyyyMMddHHmmss");
string filePath = "~/App_Data/Export/" + dashboardID + "_" + dateTimeNow + ".pdf";
ASPxDashboardExporter exporter = new ASPxDashboardExporter(DashboardConfigurator.Default);
exporter.ExportToPdf(dashboardID, stream, new System.Drawing.Size(1024, 768), dashboardState, pdfOptions);
SaveFile(stream, filePath);
ContentResult result = new ContentResult();
result.Content = filePath;
return result;
}
}
private void SaveFile(MemoryStream stream, string path) {
var fileStream = System.IO.File.Create(Server.MapPath(path));
stream.WriteTo(fileStream);
fileStream.Close();
}
}
}
function onBeforeRender(sender) {
var control = sender.getDashboardControl();
control.registerExtension(new DevExpress.Dashboard.DashboardPanelExtension(control));
$("#buttonContainer").dxButton({
text: "Export to PDF",
onClick: function (param) {
var dashboardID = control.dashboardContainer().id;
var dashboardStateJson = control.dashboard().state();
$.ajax({
url: 'Home/ExportDashboardToPdf',
data: {
DashboardID: dashboardID,
DashboardState: JSON.stringify(dashboardStateJson)
},
type: 'POST',
}).success(function (result) {
DevExpress.ui.notify('A dashboard was exported to ' + result, 'success', 5000);
});
}
});
}
using DevExpress.DashboardWeb;
using DevExpress.DashboardWeb.Mvc;
using System.Web.Routing;
namespace MvcDashboard_ServerExport {
public static class DashboardConfig {
public static void RegisterService(RouteCollection routes) {
routes.MapDashboardRoute("dashboardControl");
DashboardFileStorage dashboardFileStorage = new DashboardFileStorage("~/App_Data/Dashboards");
DashboardConfigurator.Default.SetDashboardStorage(dashboardFileStorage);
}
}
}
<div id="buttonContainer" style="float: left; margin-left: 125px;"></div>
<div style="position: absolute; left: 0; right: 0; top:50px; bottom:0;">
@Html.DevExpress().Dashboard(settings => {
settings.Name = "webDashboard";
settings.WorkingMode = DevExpress.DashboardWeb.WorkingMode.Viewer;
settings.AllowExportDashboard = false;
settings.Width = Unit.Percentage(100);
settings.Height = Unit.Percentage(100);
settings.ClientSideEvents.BeforeRender = "onBeforeRender";
}).GetHtml()
</div>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Routing;
namespace MvcDashboard_ServerExport {
// Note: For instructions on enabling IIS6 or IIS7 classic mode,
// visit http://go.microsoft.com/?LinkId=9394801
public class MvcApplication : System.Web.HttpApplication {
protected void Application_Start() {
DashboardConfig.RegisterService(RouteTable.Routes);
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
ModelBinders.Binders.DefaultBinder = new DevExpress.Web.Mvc.DevExpressEditorsBinder();
DevExpress.Web.ASPxWebControl.CallbackError += Application_Error;
}
protected void Application_Error(object sender, EventArgs e) {
Exception exception = System.Web.HttpContext.Current.Server.GetLastError();
//TODO: Handle Exception
}
}
}
Imports System.IO
Imports System.Web.Mvc
Imports DevExpress.DashboardCommon
Imports DevExpress.DashboardWeb
Namespace MvcDashboard_ServerExport.Controllers
Public Class HomeController
Inherits Controller
Public Function Index() As ActionResult
Return View()
End Function
<HttpPost> _
Public Function ExportDashboardToPdf(ByVal DashboardID As String, ByVal DashboardState As String) As ActionResult
Using stream As New MemoryStream()
Dim dashboardID_Renamed As String = DashboardID
Dim dashboardState_Renamed As New DashboardState()
dashboardState_Renamed.LoadFromJson(DashboardState)
Dim pdfOptions As New DashboardPdfExportOptions()
pdfOptions.ExportFilters = True
pdfOptions.DashboardStatePosition = DashboardStateExportPosition.Below
Dim dateTimeNow As String = Date.Now.ToString("yyyyMMddHHmmss")
Dim filePath As String = "~/App_Data/Export/" & dashboardID_Renamed & "_" & dateTimeNow & ".pdf"
Dim exporter As New ASPxDashboardExporter(DashboardConfigurator.Default)
exporter.ExportToPdf(dashboardID_Renamed, stream, New System.Drawing.Size(1024, 768), dashboardState_Renamed, pdfOptions)
SaveFile(stream, filePath)
Dim result As New ContentResult()
result.Content = filePath
Return result
End Using
End Function
Private Sub SaveFile(ByVal stream As MemoryStream, ByVal path As String)
Dim fileStream = System.IO.File.Create(Server.MapPath(path))
stream.WriteTo(fileStream)
fileStream.Close()
End Sub
End Class
End Namespace
Imports System.Web.Routing
Imports DevExpress.DashboardWeb
Imports DevExpress.DashboardWeb.Mvc
Namespace MvcDashboard_ServerExport
Public NotInheritable Class DashboardConfig
Private Sub New()
End Sub
Public Shared Sub RegisterService(ByVal routes As RouteCollection)
routes.MapDashboardRoute("dashboardControl")
Dim dashboardFileStorage As New DashboardFileStorage("~/App_Data/Dashboards")
DashboardConfigurator.Default.SetDashboardStorage(dashboardFileStorage)
End Sub
End Class
End Namespace
Imports System.Web.Http
Imports System.Web.Mvc
Imports System.Web.Routing
Namespace MvcDashboard_ServerExport
' Note: For instructions on enabling IIS6 or IIS7 classic mode,
' visit http://go.microsoft.com/?LinkId=9394801
Public Class MvcApplication
Inherits System.Web.HttpApplication
Protected Sub Application_Start()
DashboardConfig.RegisterService(RouteTable.Routes)
AreaRegistration.RegisterAllAreas()
GlobalConfiguration.Configure(AddressOf WebApiConfig.Register)
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters)
RouteConfig.RegisterRoutes(RouteTable.Routes)
ModelBinders.Binders.DefaultBinder = New DevExpress.Web.Mvc.DevExpressEditorsBinder()
AddHandler DevExpress.Web.ASPxWebControl.CallbackError, AddressOf Application_Error
End Sub
Protected Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
Dim exception As Exception = System.Web.HttpContext.Current.Server.GetLastError()
'TODO: Handle Exception
End Sub
End Class
End Namespace
<div id="buttonContainer" style="float: left; margin-left: 125px;"></div>
<div style="position: absolute; left: 0; right: 0; top:50px; bottom:0;">
@Html.DevExpress().Dashboard(settings => {
settings.Name = "webDashboard";
settings.WorkingMode = DevExpress.DashboardWeb.WorkingMode.Viewer;
settings.AllowExportDashboard = false;
settings.Width = Unit.Percentage(100);
settings.Height = Unit.Percentage(100);
settings.ClientSideEvents.BeforeRender = "onBeforeRender";
}).GetHtml()
</div>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Dashboard Web Application</title>
<script src="@Url.Content("~/Scripts/DashboardExport.js")" type="text/javascript"></script>
@Html.DevExpress().GetStyleSheets(
new StyleSheet { ExtensionSuite = ExtensionSuite.Dashboard }
)
@Html.DevExpress().GetScripts(
new Script { ExtensionSuite = ExtensionSuite.Dashboard }
)
<link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
</head>
<body>
@RenderBody()
</body>
</html>
function onBeforeRender(sender) {
var control = sender.getDashboardControl();
control.registerExtension(new DevExpress.Dashboard.DashboardPanelExtension(control));
$("#buttonContainer").dxButton({
text: "Export to PDF",
onClick: function (param) {
var dashboardID = control.dashboardContainer().id;
var dashboardStateJson = control.dashboard().state();
$.ajax({
url: 'Home/ExportDashboardToPdf',
data: {
DashboardID: dashboardID,
DashboardState: JSON.stringify(dashboardStateJson)
},
type: 'POST',
}).success(function (result) {
DevExpress.ui.notify('A dashboard was exported to ' + result, 'success', 5000);
});
}
});
}