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

Using a Window to Add a New Item to a Grid

Environment

Razor
<table>
 <tr>
  <td>Product</td>
  <td>Telerik UI for ASP.NET Core Grid and Telerik UI for ASP.NET Core Window</td>
 </tr>
 <tr>
  <td>Product Version</td>
  <td>Created with version 2024.4.1112</td>
 </tr>
</table>

Description

How can I use the Window component to add a new item to the Grid?

Solution

  1. Define an external Button (for example, above or below the Grid) and handle its Click event.
  2. Define a hidden Window component.
  3. Handle the Click event of the Button.
  4. Within the Click event handler of the Button, call the center() method to center the Window and then the open() method to open it.
  5. Load the Content for the Window from a Partial View ("_OrderCreate").
  6. When submitting the form that is displayed through the Partial View, add the new item to the Grid (Create action in the GridController).
Razor
    <div class="row">
        <div class="col-12">
            @(Html.Kendo().Grid<TelerikMvcApp5.Models.OrderViewModel>()
                .Name("grid")
                .Columns(columns =>
                {
                    columns.Bound(order => order.OrderID);
                    columns.Bound(order => order.CustomerID);
                    columns.Bound(order => order.ContactName);
                    columns.Bound(order => order.OrderDate).Format("{0:d}");
                    columns.Bound(order => order.ShippedDate).Format("{0:d}");
                })
                .Pageable()
                .Sortable()
                .Scrollable()
                .Filterable()
                .HtmlAttributes(new { style = "height:550px;" })
                .DataSource(dataSource => dataSource
                    .Ajax()
                    .PageSize(20)
                    .Read(read => read.Action("Orders_Read", "Grid"))
                )
            )
        </div>
    </div>
    
    <div class="col-xs-12 col-md-3">
        <h2>Add Data</h2>
        @(Html.Kendo().Button()
                    .Name("openCreateBtn")
                    .Content("Create Record")
                    .HtmlAttributes(new { type = "button" })
                    .Events(events => events.Click("openCreateDialog"))
                )
        @(Html.Kendo().Window()
                    .Name("createPopup")
                    .Title("Create Dialog")
                    .LoadContentFrom("OrderCreatePartial", "Home")
                    .Draggable(true)
                    .Resizable(resizable => resizable.Enabled(true))
                    .Visible(false)
                )
    </div>

For the complete implementation of the suggested approach, refer to the ASP.NET MVC application on how to use a Window to add a new item to a Grid. You can use this project as a starting point to configure the same setup in an ASP.NET Core project.

More ASP.NET Core Grid Resources

See Also