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..e98e28ad95 --- /dev/null +++ b/ej2-asp-core-mvc/code-snippet/grid/edit/prevent-add-duplicate/customvalidation.cs @@ -0,0 +1,5 @@ + 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..a3bec8629d --- /dev/null +++ b/ej2-asp-core-mvc/code-snippet/grid/edit/prevent-add-duplicate/razor @@ -0,0 +1,36 @@ +@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..c2f6a1daa8 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.