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..975fc41f71 --- /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..8c1532b424 --- /dev/null +++ b/ej2-asp-core-mvc/code-snippet/grid/edit/prevent-add-duplicate/razor @@ -0,0 +1,32 @@ +@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").ValidationRules(new { required = "true"}).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(); +}).EditSettings(edit => { edit.AllowAdding(true).AllowEditing(true).AllowDeleting(true); }).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..dbad5f2045 --- /dev/null +++ b/ej2-asp-core-mvc/code-snippet/grid/edit/prevent-add-duplicate/tagHelper @@ -0,0 +1,35 @@ + + + + + + + + + + + + \ 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..3a766fd8eb 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,31 @@ 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) | +## Prevent adding duplicate rows in Syncfusion ASP.NET MVC Grid with custom validation + +The Syncfusion ASP.NET MVC Grid allows you to enforce constraints to prevent duplicate rows by customizing the validation logic within the Grid setup. This ensures data integrity by restricting duplicate entries in the **OrderID** column. + +To prevent adding duplicate rows in the Grid, follow these steps: + +1. Implement Custom Validation: Define the `orderIdCustomValidation` function to check whether the entered **OrderID** already exists in the [dataSource](https://help.syncfusion.com/cr/aspnetmvc-js2/Syncfusion.EJ2.Grids.Grid.html#Syncfusion_EJ2_Grids_Grid_DataSource). This allows editing an existing row without triggering a duplicate error. + +2. Add Dynamic Validation Rules: Create the `orderIDRules` object to enforce unique **OrderID** values. Dynamically add this rule to the form during the **save** action. + +3. Handle Validation in the [ActionBegin](https://help.syncfusion.com/cr/aspnetmvc-js2/syncfusion.ej2.grids.grid.html#Syncfusion_EJ2_Grids_Grid_ActionBegin) event: In the `ActionBegin` event, check if the **requestType** is **save**. Apply the validation rule before saving and cancel the action `args.cancel = true` if the validation fails. + +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 display the Grid's tooltip validation for the server side. + +{% 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 %} + +![Prevent Duplicate row](../images/editing/prevent-duplicate-row.png) + ## 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..9a7b6caee4 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,31 @@ 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) | +## Prevent adding duplicate rows in Syncfusion ASP.NET Core Grid with custom validation + +The Syncfusion ASP.NET Core Grid allows you to enforce constraints to prevent duplicate rows by customizing the validation logic within the Grid setup. This ensures data integrity by restricting duplicate entries in the **OrderID** column. + +To prevent adding duplicate rows in the Grid, follow these steps: + +1. Implement Custom Validation: Define the `orderIdCustomValidation` function to check whether the entered **OrderID** already exists in the [dataSource](https://help.syncfusion.com/cr/aspnetcore-js2/Syncfusion.EJ2.Grids.Grid.html#Syncfusion_EJ2_Grids_Grid_DataSource). This allows editing an existing row without triggering a duplicate error. + +2. Add Dynamic Validation Rules: Create the `orderIDRules` object to enforce unique **OrderID** values. Dynamically add this rule to the form during the **save** action. + +3. Handle Validation in the [actionBegin](https://help.syncfusion.com/cr/aspnetcore-js2/syncfusion.ej2.grids.grid.html#Syncfusion_EJ2_Grids_Grid_ActionBegin) event: In the `actionBegin` event, check if the **requestType** is **save**. Apply the validation rule before saving and cancel the action `args.cancel = true` if the validation fails. + +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 display the Grid's tooltip validation for the server side. + +{% 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 %} + +![Prevent Duplicate row](../images/editing/prevent-duplicate-row.png) + ## 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/images/editing/prevent-duplicate-row.png b/ej2-asp-core-mvc/grid/images/editing/prevent-duplicate-row.png new file mode 100644 index 0000000000..ac1e78939f Binary files /dev/null and b/ej2-asp-core-mvc/grid/images/editing/prevent-duplicate-row.png differ