Getting Started with the ASP.NET MVC Sparkline Control

23 Jul 20266 minutes to read

This section briefly explains how to add the Syncfusion® ASP.NET MVC Sparkline control to your ASP.NET MVC application using Visual Studio.

Prerequisites

Refer to the System requirements for ASP.NET MVC controls before creating the application.

Create an ASP.NET MVC application with HTML helper

You can create an ASP.NET MVC application using either of the following options:

Install the ASP.NET MVC NuGet package

To add Syncfusion® ASP.NET MVC controls in the application, open the NuGet Package Manager in Visual Studio by selecting (Tools → NuGet Package Manager → Manage NuGet Packages for Solution). Search for Syncfusion.EJ2.MVC5 and install it.

Alternatively, you can use the Package Manager Console by navigating to:
Tools → NuGet Package Manager → Package Manager Console, and then run the following command:

Install-Package Syncfusion.EJ2.MVC5 -Version 34.1.29

NOTE

Syncfusion® ASP.NET MVC controls are available on nuget.org. Refer to the NuGet packages topic to learn more about installing NuGet packages in various operating system environments. The Syncfusion.EJ2.MVC5 NuGet package depends on Newtonsoft.Json for JSON serialization and Syncfusion.Licensing for validating the Syncfusion® license key.

Add the namespace

Add the Syncfusion.EJ2 namespace reference in the Web.config file available in the Views folder.

<namespaces>
    <add namespace="Syncfusion.EJ2" />
</namespaces>

Add script resources

Add the script reference inside the <head> element of the ~/Views/Shared/_Layout.cshtml file as follows.

<head>
    ...
    <!-- Syncfusion ASP.NET MVC controls scripts -->
    <script src="https://cdn.syncfusion.com/ej2/34.1.29/dist/ej2.min.js"></script>
</head>

NOTE

Refer to the Adding Script Reference topic to learn different approaches for adding script references in your ASP.NET MVC application.

Register the Syncfusion® Script Manager

Register the script manager EJS().ScriptManager() at the end of the <body> element in the ~/Views/Shared/_Layout.cshtml file as follows.

<body>
    ...
    <!-- Syncfusion ASP.NET MVC Script Manager -->
    @Html.EJS().ScriptManager()
</body>

Add the ASP.NET MVC Sparkline control

Now, add the Syncfusion® ASP.NET MVC Sparkline control in ~/Views/Home/Index.cshtml page.

<h2> Essential JS 2 for ASP.NET MVC Sparkline </h2>
@Html.EJS().Sparkline("spark").DataSource(Model).XName("xValue").YName("yValue").Height("100").Width("70").Render()
public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View(DataSource.GetData());
    }
}
public class DataSource
{
    public int x;
    public string xValue;
    public double yValue;
    public static List<DataSource> GetData()
    {
        List<DataSource> data1 = new List<DataSource>();
        data1.Add(new DataSource() { x = 0, xValue = "2005", yValue = 20090440 });
        data1.Add(new DataSource() { x = 1, xValue = "2006", yValue = 20264080 });
        data1.Add(new DataSource() { x = 2, xValue = "2007", yValue = 20434180 });
        data1.Add(new DataSource() { x = 3, xValue = "2008", yValue = 21007310 });
        data1.Add(new DataSource() { x = 4, xValue = "2009", yValue = 21262640 });
        data1.Add(new DataSource() { x = 5, xValue = "2010", yValue = 21515750 });
        data1.Add(new DataSource() { x = 6, xValue = "2011", yValue = 21766710 });
        data1.Add(new DataSource() { x = 7, xValue = "2012", yValue = 22015580 });
        data1.Add(new DataSource() { x = 8, xValue = "2013", yValue = 22262500 });
        data1.Add(new DataSource() { x = 9, xValue = "2014", yValue = 22507620 });
        return data1;
    }
}

Press Ctrl + F5 on Windows to run the application. The Syncfusion® ASP.NET MVC Sparkline control will be rendered in the default web browser.

ASP.NET MVC Sparkline Control

NOTE

View Sample in GitHub to understand how this getting started example works.

Troubleshooting

If the Sparkline control does not render as expected, review the following common issues and their resolutions.

  • “Unlicensed” watermark appears on the page — The Syncfusion.Licensing package is installed but the license key has not been registered. Register the license key in ~/App_Start/FilterConfig.cs (or Program.cs) and rebuild. See Registering the Syncfusion license key.

  • “Script Manager is not defined” or scripts run twice@Html.EJS().ScriptManager() was not added, or was added more than once. Ensure ScriptManager is registered exactly once at the end of <body> in _Layout.cshtml.

  • “Could not load file or assembly ‘Syncfusion.EJ2’“ — The NuGet package was not restored. Run Update-Package -reinstall in the Package Manager Console, or restore via Visual Studio (right-click solution → Restore NuGet Packages).

  • Chart renders but no data points appear — The XName / YName values (xValue / yValue) must match the property names in the data model exactly. Property names are case-sensitive.

  • “The model item passed is null” — The view is decorated with @model List<SparklineData> and the controller is returning null (or the model is empty). Verify the Index action returns a populated list and the namespace in the view matches the model’s namespace.

  • Newtonsoft.Json reference error after upgradeSyncfusion.EJ2.MVC5 requires a specific Newtonsoft.Json version. Install a compatible Newtonsoft.Json version (≥ 12.0.2) via NuGet.

See also