Getting Started

17 Feb 202210 minutes to read

This section briefly explains about how to include a simple Accordion in your ASP.NET MVC application. You can refer ASP.NET MVC Getting Started documentation page for introduction part of the system requirements and configure the common specifications.

Adding component to the Application

  • Now open your view page to render Accordion component.
@using Syncfusion.EJ2.Navigations;

@(Html.EJS().Accordion("defaultAccordion")
    .Items(new List<AccordionAccordionItem> {
        new AccordionAccordionItem { Header = "ASP.NET", Expanded = true, Content = "Microsoft ASP.NET is a set of technologies in the Microsoft .NET Framework for building Web applications and XML Web services. ASP.NET pages execute on the server and generate markup such as HTML, WML, or XML that is sent to a desktop or mobile browser. ASP.NET pages use a compiled,event-driven programming model that improves performance and enables the separation of application logic and user interface." },
        new AccordionAccordionItem { Header = "ASP.NET MVC", Content = "The Model-View-Controller (MVC) architectural pattern separates an application into three main components: the model, the view, and the controller. The ASP.NET MVC framework provides an alternative to the ASP.NET Web Forms pattern for creating Web applications. The ASP.NET MVC framework is a lightweight, highly testable presentation framework that (as with Web Forms-based applications) is integrated with existing ASP.NET features, such as master pages and membership-based authentication." },
        new AccordionAccordionItem { Header = "JavaScript", Content = "JavaScript (JS) is an interpreted computer programming language.It was originally implemented as part of web browsers so that client-side scripts could interact with the user, control the browser, communicate asynchronously, and alter the document content that was displayed.More recently, however, it has become common in both game development and the creation of desktop applications." }
    }).Render()
)
public ActionResult Index()
{
    return View();
}

Output be like the below.

Default accordion

Render the Accordion using content template

You can bind any data in Accordion items, by simply using the content template property in ASP.NET Accordion. Accordion is already provided with the content template support and hence we can utilize this support to load the other HTML elements or as per your requirement

In the below demo, the Accordion items are given with chart, grid, calender as their content using the content template.

@using Syncfusion.EJ2.Charts;
@using Syncfusion.EJ2.Navigations;

<div>
    Content Template
</div>
@(Html.EJS().Accordion("defaultAccordion")
    .ContentTemplate(
        @<div class="e-accordion-container">
            <div>
                <div>
                    <div> Chart </div>
                </div>
                <div>
                    <div>
                        @(Html.EJS().Chart("container")
                            .Series(series => {
                                series.Type(ChartSeriesType.Line).Width(2).XName("xValue").Marker(mr=>mr.Visible(true).Width(10).Height(10)).YName("yValue").DataSource(ViewBag.dataSource).Name("Germany").Add();
                                series.Type(ChartSeriesType.Line).Width(2).XName("xValue").YName("yValue1").Marker(mr => mr.Visible(true).Width(10).Height(10)).DataSource(ViewBag.dataSource).Name("England").Add();
                            })
                            .Title("Inflation - Consumer Price")
                            .Render()
                        )
                    </div>
                </div>
            </div>
            <div>
                <div>
                    <div> Grid </div>
                </div>
                <div>
                    <div>
                        @(Html.EJS().Grid("ej2grid")
                            .Height("400px")
                            .DataSource(dataManger => {
                                dataManger.Url("https://services.odata.org/V4/Northwind/Northwind.svc/Products").CrossDomain(true).Adaptor("ODataV4Adaptor");
                            })
                            .Columns(col => {
                                col.Field("ProductID").HeaderText("Product ID").Width("120").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
                                col.Field("ProductName").HeaderText("Product Name").Width("150").Add();
                                col.Field("UnitPrice").HeaderText("Supplier ID").Width("130").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
                                col.Field("UnitsInStock").HeaderText("QuantityPerUnit").Width("120").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
                                col.Field("Discontinued").HeaderText("Discontinued").Width("140").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Center).Type("boolean").DisplayAsCheckBox(true).Add();
                            })
                            .AllowPaging()
                            .Render()
                        )
                    </div>
                </div>
            </div>
            <div>
                <div>
                    <div> Calendar </div>
                </div>
                <div>
                    <div>
                        @Html.EJS().Calendar("calnder").Render()
                    </div>
                </div>
            </div>
        </div>
    )
    .Render()
)
public ActionResult Index()
{
    List<LineChartData> chartData = new List<LineChartData>
    {
        new LineChartData { xValue = new DateTime(2005, 01, 01), yValue = 21, yValue1 = 28 },
        new LineChartData { xValue = new DateTime(2006, 01, 01), yValue = 24, yValue1 = 44 },
        new LineChartData { xValue = new DateTime(2007, 01, 01), yValue = 36, yValue1 = 48 },
        new LineChartData { xValue = new DateTime(2008, 01, 01), yValue = 38, yValue1 = 50 },
        new LineChartData { xValue = new DateTime(2009, 01, 01), yValue = 54, yValue1 = 66 },
        new LineChartData { xValue = new DateTime(2010, 01, 01), yValue = 57, yValue1 = 78 },
        new LineChartData { xValue = new DateTime(2011, 01, 01), yValue = 70, yValue1 = 84 },
    };
    ViewBag.dataSource = chartData;
    return View();
}

public class LineChartData
{
    public DateTime xValue;
    public double yValue;
    public double yValue1;
}

Output be like the below.

content template

You can also render accordion without using ContentTemplate which can be referred here.