The sample illustrates how to save the current ASP.NET MVC Dashboard state (such as master filter or parameter values) to cookies on the client side and restore this state on the server side. The following API is used in this example:
using DevExpress.DashboardCommon;
using DevExpress.DashboardWeb;
using System.Web;
using System.Xml.Linq;
namespace MvcDashboard_DashboardStateCookies {
internal class CustomDashboardStateService : IDashboardStateService {
public DashboardState GetState(string dashboardId, XDocument dashboard) {
HttpCookie cookie = HttpContext.Current.Request.Cookies["MVCxDashboardState"];
if (cookie != null) {
DashboardState dashboardState = new DashboardState();
dashboardState.LoadFromJson(HttpUtility.UrlDecode(cookie.Value));
return dashboardState;
}
else
return null;
}
}
}
using System;
using System.Web.Mvc;
using System.Web.Routing;
namespace MvcDashboard_DashboardStateCookies {
public class MvcApplication : System.Web.HttpApplication {
protected void Application_Start() {
DashboardConfig.RegisterService(RouteTable.Routes);
AreaRegistration.RegisterAllAreas();
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();
}
}
}
using System.Web.Mvc;
namespace MvcDashboard_DashboardStateCookies.Controllers {
public class HomeController : Controller {
public ActionResult Index() {
return View();
}
}
}
<script type="text/javascript">
function onDashboardStateChanged(s, e) {
var cookies = e.DashboardState;
ASPxClientUtils.SetCookie('MVCxDashboardState', cookies);
}
</script>
@Html.DevExpress().Dashboard(settings => {
settings.Name = "Dashboard";
settings.WorkingMode = DevExpress.DashboardWeb.WorkingMode.ViewerOnly;
settings.Width = Unit.Percentage(100);
settings.Height = Unit.Percentage(100);
settings.ClientSideEvents.DashboardStateChanged = "onDashboardStateChanged";
}).GetHtml()
using DevExpress.DashboardWeb;
using DevExpress.DashboardWeb.Mvc;
using System.Web.Routing;
namespace MvcDashboard_DashboardStateCookies {
public static class DashboardConfig {
public static void RegisterService(RouteCollection routes) {
routes.MapDashboardRoute("dashboardControl");
DashboardFileStorage dashboardFileStorage = new DashboardFileStorage("~/App_Data/Dashboards");
DashboardConfigurator.Default.SetDashboardStorage(dashboardFileStorage);
DashboardConfigurator.Default.SetDashboardStateService(new CustomDashboardStateService());
}
}
}
Imports System.Web.Mvc
Namespace MvcDashboard_DashboardStateCookies.Controllers
Public Class HomeController
Inherits Controller
Public Function Index() As ActionResult
Return View()
End Function
End Class
End Namespace
Imports System.Web.Routing
Imports DevExpress.DashboardWeb
Imports DevExpress.DashboardWeb.Mvc
Namespace MvcDashboard_DashboardStateCookies
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)
DashboardConfigurator.Default.SetDashboardStateService(New CustomDashboardStateService())
End Sub
End Class
End Namespace
Imports DevExpress.DashboardCommon
Imports DevExpress.DashboardWeb
Namespace MvcDashboard_DashboardStateCookies
Friend Class CustomDashboardStateService
Implements IDashboardStateService
Public Function GetState(ByVal dashboardId As String, ByVal dashboard As XDocument) As DashboardState Implements IDashboardStateService.GetState
Dim cookie As HttpCookie = HttpContext.Current.Request.Cookies("MVCxDashboardState")
If cookie IsNot Nothing Then
Dim dashboardState As New DashboardState()
dashboardState.LoadFromJson(HttpUtility.UrlDecode(cookie.Value))
Return dashboardState
Else
Return Nothing
End If
End Function
End Class
End Namespace
<script type="text/javascript">
function onDashboardStateChanged(s, e) {
var cookies = e.DashboardState;
ASPxClientUtils.SetCookie('MVCxDashboardState', cookies);
}
</script>
@Html.DevExpress().Dashboard(settings => {
settings.Name = "Dashboard";
settings.WorkingMode = DevExpress.DashboardWeb.WorkingMode.ViewerOnly;
settings.Width = Unit.Percentage(100);
settings.Height = Unit.Percentage(100);
settings.ClientSideEvents.DashboardStateChanged = "onDashboardStateChanged";
}).GetHtml()
Imports System.Web.Mvc
Imports System.Web.Routing
Namespace MvcDashboard_DashboardStateCookies
Public Class MvcApplication
Inherits System.Web.HttpApplication
Protected Sub Application_Start()
DashboardConfig.RegisterService(RouteTable.Routes)
AreaRegistration.RegisterAllAreas()
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()
End Sub
End Class
End Namespace