From 9fbacf08918c948666ef342074390fc530378fc9 Mon Sep 17 00:00:00 2001 From: Gayathri4135 Date: Wed, 19 Feb 2025 18:09:43 +0530 Subject: [PATCH 1/2] Documentation(940681-dev)-Need to Add topic how to prevent of adding duplicate row in Grid MVC & Core platform --- .../prevent-add-duplicate/customvalidation.cs | 7 ++++ .../grid/edit/prevent-add-duplicate/razor | 37 +++++++++++++++++++ .../grid/edit/prevent-add-duplicate/tagHelper | 37 +++++++++++++++++++ .../grid/EJ2_ASP.MVC/editing/edit.md | 17 +++++++++ .../grid/EJ2_ASP.NETCORE/editing/edit.md | 17 +++++++++ 5 files changed, 115 insertions(+) create mode 100644 ej2-asp-core-mvc/code-snippet/grid/edit/prevent-add-duplicate/customvalidation.cs create mode 100644 ej2-asp-core-mvc/code-snippet/grid/edit/prevent-add-duplicate/razor create mode 100644 ej2-asp-core-mvc/code-snippet/grid/edit/prevent-add-duplicate/tagHelper diff --git a/ej2-asp-core-mvc/code-snippet/grid/edit/prevent-add-duplicate/customvalidation.cs b/ej2-asp-core-mvc/code-snippet/grid/edit/prevent-add-duplicate/customvalidation.cs new file mode 100644 index 0000000000..3a7eda6e7f --- /dev/null +++ b/ej2-asp-core-mvc/code-snippet/grid/edit/prevent-add-duplicate/customvalidation.cs @@ -0,0 +1,7 @@ + public IActionResult Index() + { + ViewBag.DataSource = OrdersDetails.GetAllRecords(); + return View(); + } + + \ No newline at end of file diff --git a/ej2-asp-core-mvc/code-snippet/grid/edit/prevent-add-duplicate/razor b/ej2-asp-core-mvc/code-snippet/grid/edit/prevent-add-duplicate/razor new file mode 100644 index 0000000000..532a065bfe --- /dev/null +++ b/ej2-asp-core-mvc/code-snippet/grid/edit/prevent-add-duplicate/razor @@ -0,0 +1,37 @@ +@Html.EJS().Grid("CustomValid").DataSource((IEnumerable)ViewBag.DataSource).Columns(col => +{ + col.Field("OrderID").HeaderText("Order ID").IsPrimaryKey(true).Width("120").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add(); + col.Field("CustomerID").HeaderText("Customer Name").Width("150").Add(); + col.Field("Freight").HeaderText("Freight").Width("120").EditType("numericedit").Format("C2").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add(); + col.Field("ShipName").HeaderText("Ship Name").Width("150").Add(); + col.Field("ShipCountry").HeaderText("Ship Country").EditType("dropdownedit").Width("150").Add(); + +}).AllowPaging().EditSettings(edit => { edit.AllowAdding(true).AllowEditing(true).AllowDeleting(true); }).Load("load").Created("created").ActionBegin("actionBegin").Toolbar(new List() { "Add", "Edit", "Delete", "Update", "Cancel" }).Render() + + \ No newline at end of file diff --git a/ej2-asp-core-mvc/code-snippet/grid/edit/prevent-add-duplicate/tagHelper b/ej2-asp-core-mvc/code-snippet/grid/edit/prevent-add-duplicate/tagHelper new file mode 100644 index 0000000000..92201f34cb --- /dev/null +++ b/ej2-asp-core-mvc/code-snippet/grid/edit/prevent-add-duplicate/tagHelper @@ -0,0 +1,37 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/ej2-asp-core-mvc/grid/EJ2_ASP.MVC/editing/edit.md b/ej2-asp-core-mvc/grid/EJ2_ASP.MVC/editing/edit.md index 34535af423..21a7443c8b 100644 --- a/ej2-asp-core-mvc/grid/EJ2_ASP.MVC/editing/edit.md +++ b/ej2-asp-core-mvc/grid/EJ2_ASP.MVC/editing/edit.md @@ -192,6 +192,23 @@ In the following code example, the Employee Name is a foreign key column. When e | -------------- | ------------- | | ![Foreign key column edit](../images/editing/on-foreign-key-column-editing.png) | ![After foreign key column edit](../images/editing/after-foreign-key-column-editing.png) | +## How to prevent adding duplicate rows in Grid with custom validation + +The Syncfusion Grid allows you to enforce constraints to prevent the addition of duplicate rows by customizing the validation logic directly within the Grid setup. This customization is achieved by handling custom validation rules or by implementing a custom validation function within the [ActionBegin](https://help.syncfusion.com/cr/aspnetmvc-js2/syncfusion.ej2.grids.grid.html#Syncfusion_EJ2_Grids_Grid_ActionBegin) event specifically for the `save` **requestType**. This approach allows you to intercept the save action and cancel it if necessary through the [ActionBegin](https://help.syncfusion.com/cr/aspnetmvc-js2/syncfusion.ej2.grids.grid.html#Syncfusion_EJ2_Grids_Grid_ActionBegin) event arguments. + +For server-side validation to prevent adding duplicate rows, you can refer to the detailed guidance provided in our [knowledge base](https://support.syncfusion.com/kb/article/11608/how-to-do-server-side-validation-for-grid-in-asp-net-mvc-application). If you want to display the Grid's validation tooltip instead of the alert used in our knowledge base, you can call the `grid.editModule.formObj.validate()` method on `Ajax/Fetch` success function to display the Grid's tooltip validation for the server side. + +In the following code example, the OrderID serves as a primary key column. When attempting to add a new row, the Grid employs custom validation to ensure that duplicate OrderID values are not added. This validation includes displaying a tooltip message to provide immediate feedback to the user regarding the validation status. + +{% tabs %} +{% highlight razor tabtitle="CSHTML" %} +{% include code-snippet/grid/edit/prevent-add-duplicate/razor %} +{% endhighlight %} +{% highlight c# tabtitle="Edit-temp.cs" %} +{% include code-snippet/grid/edit/prevent-add-duplicate/customvalidation.cs %} +{% endhighlight %} +{% endtabs %} + ## How to perform CRUD action externally Performing CRUD (Create, Read, Update, Delete) actions externally in the Syncfusion® Grid allows you to manipulate grid data outside the grid itself. This can be useful in scenarios where you want to manage data operations programmatically. diff --git a/ej2-asp-core-mvc/grid/EJ2_ASP.NETCORE/editing/edit.md b/ej2-asp-core-mvc/grid/EJ2_ASP.NETCORE/editing/edit.md index fe38a58ef7..12f6c8555e 100644 --- a/ej2-asp-core-mvc/grid/EJ2_ASP.NETCORE/editing/edit.md +++ b/ej2-asp-core-mvc/grid/EJ2_ASP.NETCORE/editing/edit.md @@ -192,6 +192,23 @@ In the following code example, the Employee Name is a foreign key column. When e | -------------- | ------------- | | ![Foreign key column edit](../images/editing/on-foreign-key-column-editing.png) | ![After foreign key column edit](../images/editing/after-foreign-key-column-editing.png) | +## How to prevent adding duplicate rows in Grid with custom validation + +The Syncfusion Grid allows you to enforce constraints to prevent the addition of duplicate rows by customizing the validation logic directly within the Grid setup. This customization is achieved by handling custom validation rules or by implementing a custom validation function within the [ActionBegin](https://help.syncfusion.com/cr/aspnetcore-js2/syncfusion.ej2.grids.grid.html#Syncfusion_EJ2_Grids_Grid_ActionBegin) event specifically for the `save` **requestType**. This approach allows you to intercept the save action and cancel it if necessary through the [ActionBegin](https://help.syncfusion.com/cr/aspnetcore-js2/syncfusion.ej2.grids.grid.html#Syncfusion_EJ2_Grids_Grid_ActionBegin) event arguments. + +For server-side validation to prevent adding duplicate rows, you can refer to the detailed guidance provided in our [knowledge base](https://support.syncfusion.com/kb/article/11608/how-to-do-server-side-validation-for-grid-in-asp-net-mvc-application). If you want to display the Grid's validation tooltip instead of the alert used in our knowledge base, you can call the `grid.editModule.formObj.validate()` method in the Ajax/Fetch success function to trigger the Grid's tooltip validation on the server side. + +In the following code example, the OrderID serves as a primary key column. When attempting to add a new row, the grid employs custom validation to ensure that duplicate OrderID values are not added. This validation includes displaying a tooltip message to provide immediate feedback to the user regarding the validation status. + +{% tabs %} +{% highlight cshtml tabtitle="CSHTML" %} +{% include code-snippet/grid/edit/prevent-add-duplicate/tagHelper %} +{% endhighlight %} +{% highlight c# tabtitle="Edit-temp.cs" %} +{% include code-snippet/grid/edit/prevent-add-duplicate/customvalidation.cs %} +{% endhighlight %} +{% endtabs %} + ## How to perform CRUD action externally Performing CRUD (Create, Read, Update, Delete) actions externally in the Syncfusion® Grid allows you to manipulate grid data outside the grid itself. This can be useful in scenarios where you want to manage data operations programmatically. From a9ba1851df126d50137f57cdce3446f88cf8a5fc Mon Sep 17 00:00:00 2001 From: Gayathri4135 Date: Thu, 20 Feb 2025 10:48:25 +0530 Subject: [PATCH 2/2] Aligned the code --- .../grid/edit/prevent-add-duplicate/customvalidation.cs | 4 +--- .../code-snippet/grid/edit/prevent-add-duplicate/razor | 1 - ej2-asp-core-mvc/grid/EJ2_ASP.NETCORE/editing/edit.md | 4 ++-- 3 files changed, 3 insertions(+), 6 deletions(-) diff --git a/ej2-asp-core-mvc/code-snippet/grid/edit/prevent-add-duplicate/customvalidation.cs b/ej2-asp-core-mvc/code-snippet/grid/edit/prevent-add-duplicate/customvalidation.cs index 3a7eda6e7f..e98e28ad95 100644 --- a/ej2-asp-core-mvc/code-snippet/grid/edit/prevent-add-duplicate/customvalidation.cs +++ b/ej2-asp-core-mvc/code-snippet/grid/edit/prevent-add-duplicate/customvalidation.cs @@ -2,6 +2,4 @@ public IActionResult Index() { ViewBag.DataSource = OrdersDetails.GetAllRecords(); return View(); - } - - \ No newline at end of file + } \ No newline at end of file diff --git a/ej2-asp-core-mvc/code-snippet/grid/edit/prevent-add-duplicate/razor b/ej2-asp-core-mvc/code-snippet/grid/edit/prevent-add-duplicate/razor index 532a065bfe..a3bec8629d 100644 --- a/ej2-asp-core-mvc/code-snippet/grid/edit/prevent-add-duplicate/razor +++ b/ej2-asp-core-mvc/code-snippet/grid/edit/prevent-add-duplicate/razor @@ -5,7 +5,6 @@ col.Field("Freight").HeaderText("Freight").Width("120").EditType("numericedit").Format("C2").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add(); col.Field("ShipName").HeaderText("Ship Name").Width("150").Add(); col.Field("ShipCountry").HeaderText("Ship Country").EditType("dropdownedit").Width("150").Add(); - }).AllowPaging().EditSettings(edit => { edit.AllowAdding(true).AllowEditing(true).AllowDeleting(true); }).Load("load").Created("created").ActionBegin("actionBegin").Toolbar(new List() { "Add", "Edit", "Delete", "Update", "Cancel" }).Render()