Articles in this section
Category / Section

How to save edited records without showing confirmation message dialog in ASP.NET MVC Grid?

3 mins read

To save the unsaved edited records on any Grid action, in batch edit mode.

Solution               

In Batch Edit Mode, with edited records not saved, if we perform any Grid actions such as paging, sorting, etc., an alert message would be shown to save the edited record or cancel the Grid action as in the below screenshot.

We can avoid or prevent this alert message by using “ShowConfirmDialog” Grid EditSettings API.

But preventing the alert message will result in losing the edited records on performing Grid action like paging.

To prevent the alert message and at the same time to save the edited records, we can make use “ActionBegin Grid event in which using “batchSave“ Grid method we can save the edited records.

 

JS

<script type="text/javascript">
        $(function () {
            $("#Grid").ejGrid({
 
                dataSource: window.gridData,
                
                allowPaging: true,
                
                editSettings: { allowEditing: true, allowAdding: true, allowDeleting: true, editMode: "batch" },
                
                toolbarSettings: { showToolbar: true, toolbarItems: [ej.Grid.ToolBarItems.Add, ej.Grid.ToolBarItems.Edit, ej.Grid.ToolBarItems.Delete, ej.Grid.ToolBarItems.Update, ej.Grid.ToolBarItems.Cancel] },
                
                actionBegin: “begin”,
                
                columns: [
                        { field: "OrderID", isPrimaryKey: true, headerText: "Order ID", textAlign: ej.TextAlign.Right, width: 90 },
                        { field: "CustomerID", headerText: 'Customer ID', validationRules: { required: true,}, width: 90 },
                        { field: "EmployeeID", headerText: 'Employee ID', editType: ej.Grid.EditingType.Dropdown, textAlign: ej.TextAlign.Right, width: 80 },
                        { field: "Freight", headerText: 'Freight', textAlign: ej.TextAlign.Right, editType: ej.Grid.EditingType.Numeric, width: 80, format: "{0:C}" },
                        { field: "ShipName", headerText: 'Ship Name', width: 150 },
                        { field: "ShipCountry", headerText: 'Ship Country', editType: ej.Grid.EditingType.Dropdown, width: 90 }
                ]
            });
        });
    </script>

 

MVC

@(Html.EJ().Grid<object>("Editing")
        .Datasource((IEnumerable<object>)ViewBag.datasource)
 
        .EditSettings(edit => { edit.AllowAdding().AllowDeleting().AllowEditing().EditMode(EditMode.Batch).ShowConfirmDialog(false); })
 
        .ToolbarSettings(toolbar =>
        {
            toolbar.ShowToolbar().ToolbarItems(items =>
            {
                items.AddTool(ToolBarItems.Add);
                items.AddTool(ToolBarItems.Edit);
                items.AddTool(ToolBarItems.Delete);
                items.AddTool(ToolBarItems.Update);
                items.AddTool(ToolBarItems.Cancel);
            });
        })
 
        .AllowPaging()
 
        .Columns(col =>
        {
            col.Field("OrderID").HeaderText("Order ID").IsPrimaryKey(true).TextAlign(TextAlign.Right).Width(90).Add();
            col.Field("CustomerID").HeaderText("Customer ID").ValidationRules(v => v.AddRule("required", true)).Width(90).Add();
            col.Field("EmployeeID").HeaderText("Employee ID").EditType(EditingType.Dropdown).TextAlign(TextAlign.Right).Width(80).Add();
            col.Field("Freight").HeaderText("Freight").TextAlign(TextAlign.Right).Width(80).EditType(EditingType.Numeric).Format("{0:C}").Add();
            col.Field("ShipName").HeaderText("ShipName").Width(150).Add();
            col.Field("ShipCountry").HeaderText("ShipCountry").EditType(EditingType.Dropdown).Width(90).Add();
        })
 
        .ClientSideEvents(eve => eve.ActionBegin("begin"))
        )

 

ASP.NET

<ej:Grid ID="OrdersGrid" runat="server" AllowPaging="True">
 
            <Columns>
                <ej:Column Field="OrderID" HeaderText="Order ID" IsPrimaryKey="true" TextAlign="Right" Width="75" />
                <ej:Column Field="CustomerID" HeaderText="Customer ID" Width="80" >
                    <ValidationRule>
                        <ej:KeyValue Key="required" Value="true" />
                    </ValidationRule>
                </ej:Column>
                <ej:Column Field="EmployeeID" HeaderText="Employee ID" TextAlign="Right" Width="80" EditType="Dropdown" />
                <ej:Column Field="Freight" HeaderText="Freight" TextAlign="Right" Width="80" Format="{0:C}" EditType="Numeric" />
                <ej:Column Field="ShipName" HeaderText="ShipName" Width="150" />
                <ej:Column Field="ShipCountry" HeaderText="ShipCountry" Width="90" EditType="Dropdown" />
            </Columns>
 
            <EditSettings AllowEditing="True" AllowAdding="True" AllowDeleting="True" EditMode="Batch"></EditSettings>
 
            <ToolbarSettings ShowToolbar="True" ToolbarItems="add,edit,delete,update,cancel"></ToolbarSettings>
 
            <ClientSideEvents ActionBegin="begin" />
 
        </ej:Grid>

 

<script>
    function begin(args) {
       
         if (args.requestType == "paging" | args.requestType == "sorting") {
        
              if (this.model.isEdit && !this.getContent().find("#EditingEditForm").validate().form()) {
          
                  args.cancel = true; // Preventing Grid action in case of any validation error
            }
            
         var changes = this.getBatchChanges();// getBatchChanges method returns unsaved edited, added and deleted records.
 
         
         if (!args.cancel && (this.model.isEdit || changes.added.length || changes.deleted.length || changes.changed.length)) {
      
             this.batchSave(); // Saving edited records
 
            }
        }
    }
    
</script>

 

 

Conclusion

I hope you enjoyed learning about how to save edited records without showing confirmation message dialog.

You can refer to our ASP.NET MVC Grid feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our ASP.NET MVC Grid example to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments
Please sign in to leave a comment
Access denied
Access denied