How to: Bind ASP.NET MVC Dashboard to an Object Data Source and Provide Data using DataLoading
- 3 minutes to read
This example shows how to bind the ASP.NET MVC Dashboard extension to the Object Data Source and supply it with data using the DashboardConfigurator.DataLoading event. To use the created data source, run the application and create a new dashboard.
- Index.cshtml (C#)
- _Layout.cshtml (C#)
- DashboardConfig.cs
- DashboardConfig.vb
- Index.cshtml (VB.NET)
- _Layout.cshtml (VB.NET)
@{
ViewBag.Title = "Index";
}
<div style="position: absolute; left: 0; right: 0; top:0; bottom:0;">
@Html.DevExpress().Dashboard(settings => {
settings.Name = "Dashboard";
settings.Width = Unit.Percentage(100);
settings.Height = Unit.Percentage(100);
}).GetHtml()
</div>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>@ViewBag.Title</title>
@Html.DevExpress().GetStyleSheets(
new StyleSheet { ExtensionSuite = ExtensionSuite.NavigationAndLayout },
new StyleSheet { ExtensionSuite = ExtensionSuite.Editors },
new StyleSheet { ExtensionSuite = ExtensionSuite.GridView },
new StyleSheet { ExtensionSuite = ExtensionSuite.Dashboard }
)
@Html.DevExpress().GetScripts(
new Script { ExtensionSuite = ExtensionSuite.NavigationAndLayout },
new Script { ExtensionSuite = ExtensionSuite.Editors },
new Script { ExtensionSuite = ExtensionSuite.GridView },
new Script { ExtensionSuite = ExtensionSuite.Dashboard }
)
</head>
<body>
@RenderBody()
</body>
</html>
using DevExpress.DashboardCommon;
using DevExpress.DashboardWeb;
using DevExpress.DashboardWeb.Mvc;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Web.Routing;
public class DashboardConfig {
public static void RegisterService(RouteCollection routes) {
routes.MapDashboardRoute();
DashboardConfigurator.Default.SetDashboardStorage(new DashboardFileStorage(@"~/App_Data/"));
var dataSourceStorage = new DataSourceInMemoryStorage();
DashboardConfigurator.Default.SetDataSourceStorage(dataSourceStorage);
DashboardObjectDataSource objDataSource = new DashboardObjectDataSource("Object Data Source");
objDataSource.DataSource = typeof(SalesPersonData);
dataSourceStorage.RegisterDataSource("objDataSource", objDataSource.SaveToXml());
DashboardConfigurator.Default.DataLoading += Default_DataLoading;
}
private static void Default_DataLoading(object sender, DataLoadingWebEventArgs e) {
if (e.DataSourceName == "Object Data Source") {
e.Data = CreateData();
}
}
public static List<SalesPersonData> CreateData() {
List<SalesPersonData> data = new List<SalesPersonData>();
string[] salesPersons = { "Andrew Fuller", "Michael Suyama",
"Robert King", "Nancy Davolio",
"Margaret Peacock", "Laura Callahan",
"Steven Buchanan", "Janet Leverling" };
for (int i = 0; i < 100; i++) {
SalesPersonData record = new SalesPersonData();
int seed = (int)DateTime.Now.Ticks & 0x0000FFFF;
record.SalesPerson = salesPersons[new Random(seed).Next(0, salesPersons.Length)];
record.Quantity = new Random(seed).Next(0, 100);
data.Add(record);
Thread.Sleep(3);
}
return data;
}
}
public class SalesPersonData {
public string SalesPerson { get; set; }
public int Quantity { get; set; }
}
Imports System.Threading
Imports System.Web.Routing
Imports DevExpress.DashboardCommon
Imports DevExpress.DashboardWeb
Imports DevExpress.DashboardWeb.Mvc
Public Class DashboardConfig
Public Shared Sub RegisterService(ByVal routes As RouteCollection)
routes.MapDashboardRoute()
DashboardConfigurator.Default.SetDashboardStorage(New DashboardFileStorage("~/App_Data/"))
Dim dataSourceStorage = New DataSourceInMemoryStorage()
DashboardConfigurator.Default.SetDataSourceStorage(dataSourceStorage)
Dim objDataSource As New DashboardObjectDataSource("Object Data Source")
objDataSource.DataSource = GetType(SalesPersonData)
dataSourceStorage.RegisterDataSource("objDataSource", objDataSource.SaveToXml())
AddHandler DashboardConfigurator.Default.DataLoading, AddressOf Default_DataLoading
End Sub
Private Shared Sub Default_DataLoading(ByVal sender As Object, ByVal e As DataLoadingWebEventArgs)
If e.DataSourceName = "Object Data Source" Then
e.Data = CreateData()
End If
End Sub
Public Shared Function CreateData() As List(Of SalesPersonData)
Dim data As New List(Of SalesPersonData)()
Dim salesPersons() As String = {"Andrew Fuller", "Michael Suyama",
"Robert King", "Nancy Davolio",
"Margaret Peacock", "Laura Callahan",
"Steven Buchanan", "Janet Leverling"}
For i As Integer = 0 To 99
Dim record As New SalesPersonData()
Dim seed As Long = CLng(Date.Now.Ticks) And &HFFFF
record.SalesPerson = salesPersons((New Random(seed)).Next(0, salesPersons.Length))
record.Quantity = (New Random(seed)).Next(0, 100)
data.Add(record)
Thread.Sleep(3)
Next i
Return data
End Function
End Class
Public Class SalesPersonData
Public Property SalesPerson() As String
Public Property Quantity() As Integer
End Class
@{
ViewBag.Title = "Index";
}
<div style="position: absolute; left: 0; right: 0; top:0; bottom:0;">
@Html.DevExpress().Dashboard(settings => {
settings.Name = "Dashboard";
settings.Width = Unit.Percentage(100);
settings.Height = Unit.Percentage(100);
}).GetHtml()
</div>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>@ViewBag.Title</title>
@Html.DevExpress().GetStyleSheets(
new StyleSheet { ExtensionSuite = ExtensionSuite.NavigationAndLayout },
new StyleSheet { ExtensionSuite = ExtensionSuite.Editors },
new StyleSheet { ExtensionSuite = ExtensionSuite.GridView },
new StyleSheet { ExtensionSuite = ExtensionSuite.Dashboard }
)
@Html.DevExpress().GetScripts(
new Script { ExtensionSuite = ExtensionSuite.NavigationAndLayout },
new Script { ExtensionSuite = ExtensionSuite.Editors },
new Script { ExtensionSuite = ExtensionSuite.GridView },
new Script { ExtensionSuite = ExtensionSuite.Dashboard }
)
</head>
<body>
@RenderBody()
</body>
</html>