New to Telerik UI for ASP.NET Core? Start 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
- Define an external Button (for example, above or below the Grid) and handle its
Click
event. - Define a hidden Window component.
- Handle the
Click
event of the Button. - Within the
Click
event handler of the Button, call thecenter()
method to center the Window and then theopen()
method to open it. - Load the Content for the Window from a Partial View ("_OrderCreate").
- When submitting the form that is displayed through the Partial View, add the new item to the Grid (
Create
action in theGridController
).
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.