# Prevent Grid Popup Editor from Closing on Update and Create

## Environment

<table>
 <tr>
  <td>Product</td>
  <td>Progress Telerik UI for ASP.NET MVC Grid</td>
 </tr>
 <tr>
  <td>Progress Telerik UI for ASP.NET MVC version</td>
  <td>Created with the 2023.2.829 version</td>
 </tr>
</table>

## Description

How can I keep the popup editor of the Telerik UI for ASP.NET MVC Grid open after I update or insert a record?

## Solution

To achieve the desired scenario:

1. Handle the [`Edit`](https://docs.telerik.com/aspnet-mvc/api/kendo.mvc.ui.fluent/grideventbuilder#editsystemstring) event of the Grid and attach an event handler for the [`close`](https://docs.telerik.com/kendo-ui/api/ui/window/events/close) event of the Popup window.
2. In the `close` handler, the [`e.preventDefault()`](https://api.jquery.com/event.preventdefault/) method will be called to prevent the popup from closing.
3. To allow the user to close the editor, set a `preventCloseOnSave` flag when the **Cancel** and **Close** buttons are clicked.
4. Contrary to the previous step, subscribe to the [`Save`](https://docs.telerik.com/aspnet-mvc/api/kendo.mvc.ui.fluent/grideventbuilder#savesystemstring) event of the Grid and reset the flag.

```Razor Index.cshtml
@(Html.Kendo().Grid<Kendo.Mvc.Examples.Models.ProductViewModelGridPopUp>()
    .Name("grid")
    .Columns(columns =>
    {
        columns.Bound(p => p.ProductName);
        columns.Bound(p => p.UnitPrice).Width(100);
        columns.Bound(p => p.UnitsInStock).Width(100);
        columns.Bound(p => p.Discontinued).Width(100);
        columns.Command(command => { command.Edit(); command.Destroy(); }).Width(200);
    })
    .ToolBar(toolbar => toolbar.Create())
    .Editable(editable => editable.Mode(GridEditMode.PopUp))
    .Pageable()
    .Sortable()
    .Scrollable()
    .Events(ev => {
        ev.Edit("onEdit");
        ev.Save("onSave");
    })
    .HtmlAttributes(new { style = "height:430px;" })
    .DataSource(dataSource => dataSource
        .Ajax()
        .PageSize(20)
        .Model(model => model.Id(p => p.ProductID))
        .Create(update => update.Action("EditingPopup_Create", "Grid"))
        .Read(read => read.Action("EditingPopup_Read", "Grid"))
        .Update(update => update.Action("EditingPopup_Update", "Grid"))
        .Destroy(update => update.Action("EditingPopup_Destroy", "Grid"))
    )
)
```

```JS script.js
    <script type="text/javascript">
        var preventCloseOnSave = false;

        function onSave(e) {
            preventCloseOnSave = true;
        }
        var onWindowEditClose = function (e) {
            if (preventCloseOnSave) {
                e.preventDefault();
                preventCloseOnSave = false;
            }
        };

        function onEdit(e) {
             var editWindow = e.container.data("kendoWindow");
             editWindow.unbind("close");
             editWindow.bind("close", onWindowEditClose);

            $(".k-grid-cancel").on("mousedown", function (e) {

                preventCloseOnSave = false;
                editWindow.close();
            });
        }
    </script>
```

## More ASP.NET MVC Grid Resources

* [ASP.NET MVC Grid Documentation](https://www.telerik.com/aspnet-mvc/documentation/html-helpers/data-management/grid/overview)

* [ASP.NET MVC Grid Demos](https://demos.telerik.com/aspnet-mvc/grid/index)

* [ASP.NET MVC Grid Product Page](https://www.telerik.com/aspnet-mvc/grid)

* [Telerik UI for ASP.NET MVC Video Onboarding Course (Free for trial users and license holders)](https://www.telerik.com/aspnet-mvc/documentation/getting-started/video-onboarding)

* [Telerik UI for ASP.NET MVC Forums](https://www.telerik.com/forums/aspnet-mvc)

## See Also

* [Client-Side API Reference of the Grid for ASP.NET MVC](https://docs.telerik.com/kendo-ui/api/ui/grid)
* [Server-Side API Reference of the Grid for ASP.NET MVC](https://docs.telerik.com/aspnet-mvc/api/grid)
* [Server-Side TagHelper API Reference of the Grid for ASP.NET MVC](https://docs.telerik.com/aspnet-core/api/taghelpers/grid)
* [Telerik REPL: Prevent Grid Popup Editor from Closing on Update and Create](https://netcorerepl.telerik.com/QxYXQKbI285l2ekv51)
* [Telerik UI for ASP.NET MVC Breaking Changes](https://www.telerik.com/aspnet-mvc/documentation/backwards-compatibility/2023-backwards-compatibility)
* [Telerik UI for ASP.NET MVC Knowledge Base](https://docs.telerik.com/aspnet-mvc/knowledge-base)
