New to Telerik UI for ASP.NET CoreStart a free 30-day trial

Diagram in Razor Pages

Updated on Dec 10, 2025

This article describes how to seamlessly integrate and configure the Telerik UI Diagram for ASP.NET Core in Razor Pages applications.

You can use any of the available data binding approaches to bind the component to data in a Razor Pages application.

Referencing Handler Methods in Razor Pages

Razor Pages is an alternative to the MVC pattern that makes page-focused coding easier and more productive. This approach consists of a cshtml file and a cshtml.cs file (by convention, the two files have the same name).

The cshtml.cs file, known as the PageModel, contains handler methods that respond to HTTP requests. These methods are prefixed with On followed by the HTTP verb (for example, OnGet, OnPost, OnPostRead, OnPostCreate).

Handler methods declared in a PageModel can be referenced from any Razor Page using one of the following URL patterns:

  • Using Url.Page()

    C#
    Url.Page("PageName", "HandlerName")
    // OR
    Url.Page("/FolderName/PageName", "HandlerName")

    For example, Url.Page("Index", "Read") references the OnPostRead or OnGetRead handler method in the Index.cshtml.cs file.

  • Using a query string

    C#
    Url("/PathToPage?handler=HandlerName")

    For example, Url("/Index?handler=Read") references the OnPostRead or OnGetRead handler method in the Index page.

For more information on Razor Pages architecture and concepts, refer to the official Microsoft documentation.

Binding to Remote Data

To configure the CRUD operations of the Diagram within a Razor Pages application, follow the next steps:

  1. Setup CRUD URLs in the DataSource and ConnectionsDataSource configurations along with a Model.Id. The URL in these methods must refer to the name of the method in the PageModel.

    Razor
        @page
        @model DiagramEditingModel
    
        @(Html.Kendo().Diagram<OrgDiagramShape, OrgDiagramConnection>()
          .Name("diagram")
            .DataSource(d => d
              .ShapeDataSource()
              .Model(m =>
              {
                  m.Id(s => s.Id);
                  m.Field(s => s.Id).Editable(false);
                  m.Field(s => s.JobTitle);
                  m.Field(s => s.Color);
              })
              .Read(r => r.Url(Url.Page("DiagramEditing", "ReadShapes")).Data("forgeryToken"))
              .Create(r => r.Url(Url.Page("DiagramEditing", "CreateShape")).Data("forgeryToken"))
              .Destroy(r => r.Url(Url.Page("DiagramEditing", "DestroyShape")).Data("forgeryToken"))
              .Update(r => r.Url(Url.Page("DiagramEditing", "UpdateShape")).Data("forgeryToken"))
          )
          .ConnectionsDataSource(d => d
              .Model(m =>
              {
                  m.Id(c => c.Id);
                  m.Field(c => c.Id).Editable(false);
                  m.From(c => c.FromShapeId);
                  m.To(c => c.ToShapeId);
              })
              .Read(r => r.Url(Url.Page("DiagramEditing", "ReadConnections")).Data("forgeryToken"))
              .Create(r => r.Url(Url.Page("DiagramEditing", "CreateConnection")).Data("forgeryToken"))
              .Destroy(r => r.Url(Url.Page("DiagramEditing", "DestroyConnection")).Data("forgeryToken"))
              .Update(r => r.Url(Url.Page("DiagramEditing", "UpdateConnection")).Data("forgeryToken"))
          )
          ... // Additional configuration options.
        )
  2. Add an AntiForgeryToken on top of the RazorPage.

    cshtml
        @inject Microsoft.AspNetCore.Antiforgery.IAntiforgery Xsrf
        @Html.AntiForgeryToken()
  3. Send the AntiForgeryToken with each POST request of the page. Additional paratemers can also be supplied.

    JavaScript
        <script>
            function forgeryToken() {
                return kendo.antiForgeryTokens();
            }
        </script>
  4. Within the .cs file, introduce ActionMethod for each of the CRUD operations.

    C#
    public class DiagramEditingModel : PageModel
    {
        public JsonResult OnPostReadShapes([DataSourceRequest] DataSourceRequest request)
        {
            return new JsonResult(DiagramShapes.ToDataSourceResult(request));
        }
    
        public JsonResult OnPostCreateShape([DataSourceRequest] DataSourceRequest request, OrgDiagramShape shape)
        {
            shape.Id = DiagramShapes.Count + 2;
            DiagramShapes.Add(shape);
    
            return new JsonResult(new[] { shape }.ToDataSourceResult(request, ModelState));
        }
    
        public JsonResult OnPostUpdateShape([DataSourceRequest] DataSourceRequest request, OrgDiagramShape shape)
        {
            DiagramConnections.Where(x => x.Id == shape.Id).Select(x => shape);
    
            return new JsonResult(new[] { shape }.ToDataSourceResult(request, ModelState));
        }
    
        public JsonResult OnPostDestroyShape([DataSourceRequest] DataSourceRequest request, OrgDiagramShape shape)
        {
            DiagramShapes.Remove(DiagramShapes.FirstOrDefault(x => x.Id == shape.Id));
    
            return new JsonResult(new[] { shape }.ToDataSourceResult(request, ModelState));
        }
    }

For the complete project, refer to the Diagram in Razor Pages example.

See Also