Search results

Getting Started with ASP.NET MVC

Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, you also have to include a license key in your projects. Please refer to this link to know about registering Syncfusion license key in your ASP.NET MVC application to use our components.

Prerequisites

To get start with ASP.NET MVC application, need to ensure the following software to be installed on the machine.

  1. .Net Framework 4.5 and above.
  2. ASP.NET MVC 4 or ASP.NET MVC 5
  3. Visual Studio

Preparing ASP.NET MVC application

The following steps to create ASP.NET MVC Application.

Step 1: Create ASP.NET MVC Application with default template project in Visual Studio.

Default Template

Step 2: Once your project created. We need to add Syncfusion EJ2 package into your application by using NuGet Package Manager.

Open the NuGet package manager.

Solution Explorer

Install the Syncfusion.EJ2.MVC4 package to the application.

Nuget Demo

After installation complete, this will be included in the project. You can refer it from the Project Assembly Reference.

We need to install NewtonSoft.JSON as a dependency, since Syncfusion.EJ2 dependent to NewtonSoft.JSON package.

Step 3: Add Syncfusion.EJ2 namespace reference in Web.config

<namespaces>
    <add namespace="Syncfusion.EJ2"/>
</namespaces>
<system.web>
    <compilation>
      <assemblies>
        <add assembly="Syncfusion.EJ2" Version=16.4400.0.40, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
      </assemblies>
    </compilation>
  </system.web>

Step 4: Add client side resource through CDN or local package in the layout page _Layout.cshtml.

<head>
@* Syncfusion Essential JS 2 Styles *@
<link rel="stylesheet" href="https://cdn.syncfusion.com/ej2/material.css" />

@* Syncfusion Essential JS 2 Scripts *@
<script src="https://cdn.syncfusion.com/ej2/dist/ej2.min.js"></script>
</head>

Step 5: Adding Script Manager in layout page _Layout.cshtml.

@Html.EJS().ScriptManager()

Step 6: Add the below code to your Index.cshtml view page which is present under Views/Home folder, to initialize the treegrid.

razor
tree-grid.cs
@Html.EJS().TreeGrid("DefaultFunctionalities").DataSource((IEnumerable<object>)ViewBag.datasource).Columns(col =>
    {
        col.Field("TaskId").HeaderText("Task ID").Width(90).TextAlign(TextAlign.Right).Add();
        col.Field("TaskName").HeaderText("Task Name").Width(180).Add();
        col.Field("StartDate").HeaderText("Start Date").Format("yMd").TextAlign(TextAlign.Right).Width(120).Add();
        col.Field("EndDate").HeaderText("End Date").Format("yMd").TextAlign(TextAlign.Right).Width(120).Add();
        col.Field("Duration").HeaderText("Duration").Width(90).TextAlign(TextAlign.Right).Add();
        col.Field("Progress").HeaderText("Progress").Width(90).TextAlign(TextAlign.Right).Add();

    }).Height(400).ChildMapping("Children").TreeColumnIndex(1).Render()
public IActionResult Index()
{
    var Order = TreeGridItems.GetTreeData();
    ViewBag.DataSource = Order;
    return View();
}

In the above code example, the hierarchical data binding is represented in which the ChilMapping property denotes the hierarchy relationship; whereas in self-referencing data binding IdMapping and ParentIdMapping denotes the hierarchy relationship.

Enable paging

The paging feature enables users to view the treegrid record in a paged view. It can be enabled by setting the AllowPaging property to true. The pager can be customized using the PageSettings property.

In root-level paging mode, paging is based on the root-level rows only, i.e., it ignores the child row count and it can be enabled by using the PageSettings.PageSizeMode property.

razor
page.cs
@(Html.EJS().TreeGrid("Pager")
    .AllowPaging()
    .DataSource((IEnumerable<object>)ViewBag.DataSource)
    .PageSettings(page => page.PageSizes(true).PageCount(2))
    .Columns(col =>
    {
        col.Field("TaskId").HeaderText("Task ID").Width(70).TextAlign(TextAlign.Right).Add();
        col.Field("TaskName").HeaderText("Task Name").Width(200).Add();
        col.Field("StartDate").HeaderText("Start Date").Format("yMd").TextAlign(TextAlign.Right).Width(90).Add();
        col.Field("EndDate").HeaderText("End Date").Format("yMd").TextAlign(TextAlign.Right).Width(90).Add();
        col.Field("Duration").HeaderText("Duration").Width(80).TextAlign(TextAlign.Right).Add();
        col.Field("Progress").HeaderText("Progress").Width(80).TextAlign(TextAlign.Right).Add();
        col.Field("Priority").HeaderText("Priority").Width(90).TextAlign(TextAlign.Right).Add();
    })
    .ChildMapping("Children")
    .TreeColumnIndex(1)
    .Render()
)
public IActionResult Index()
{
    var Order = TreeGridItems.GetTreeData();
    ViewBag.DataSource = Order;
    return View();
}

Enable sorting

The sorting feature enables you to order the records. It can be enabled by setting the AllowSorting property to true. Sorting feature can be customized using the SortSettings property.

razor
sorting.cs
@Html.EJS().TreeGrid("Sorting").AllowSorting().DataSource((IEnumerable<object>)ViewBag.DataSource).Columns(col =>
   {
       col.Field("orderName").HeaderText("Order Name").Width(170).Add();
       col.Field("category").HeaderText("Category").Width(150).Add();
       col.Field("orderDate").HeaderText("Order Date").Format("yMd").TextAlign(TextAlign.Right).Width(130).Add();
       col.Field("units").HeaderText("Units").TextAlign(TextAlign.Right).Width(120).Add();
       col.Field("unitPrice").HeaderText("Unit Price").Width(130).TextAlign(TextAlign.Right).Add();
       col.Field("price").HeaderText("Price").Width(120).TextAlign(TextAlign.Right).Add();

   }).ChildMapping("subTasks").TreeColumnIndex(1).Render()
public IActionResult Index()
{
    var Order = TreeDataFormat.GetDataFormat();
    ViewBag.DataSource = Order;
    return View();
}

Enable filtering

The filtering feature enables you to view the reduced amount of records based on the filter criteria. It can be enabled by setting the AllowFiltering property to true. Filtering feature can be customized using the FilterSettings property.

By default, filtered records are shown along with its parent records. This behavior can be changed by using the FilterSettings-HierarchyMode property.

razor
filtering.cs
@(Html.EJS().TreeGrid("Filtering")
    .DataSource((IEnumerable<object>)ViewBag.DataSource)
    .AllowFiltering()
    .AllowPaging()
    .PageSettings(page => page.PageSize(10))
    .FilterSettings(filter =>
    {
        filter.Type(FilterType.FilterBar);
        filter.HierarchyMode(Syncfusion.EJ2.TreeGrid.FilterHierarchyMode.Parent);
    })
    .Columns(col =>
    {
        col.Field("TaskId").HeaderText("Task ID").Width(90).TextAlign(TextAlign.Right).Add();
        col.Field("TaskName").HeaderText("Task Name").Width(180).Add();
        col.Field("StartDate").HeaderText("Start Date").Format("yMd").TextAlign(TextAlign.Right).Width(120).Add();
        col.Field("Duration").HeaderText("Duration").Width(90).TextAlign(TextAlign.Right).Add();

    })
    .ChildMapping("Children")
    .TreeColumnIndex(1)
    .Render()
)
public IActionResult Index()
{
    var Order = TreeGridItems.GetTreeData();
    ViewBag.DataSource = Order;
    return View();
}

Output be like the below.

TreeGrid Sample Demo