# Batch Update

## Content



CollectionViewHelper has **BatchEdit** request that lets a user commit multiple changes to the database. The example shows how to define BatchEdit request in controller to update database with CollectionView by using BatchEdit request of [CollectionViewHelper](/componentone/api/mvc/online-mvc-core/dotnet-api/C1.AspNetCore.Mvc/C1.Web.Mvc.CollectionViewHelper.html).

The following image shows how the FlexGrid appears after setting the BatchEditing request of CollectionViewHelper. A user can commit changes to the server by clicking the **Update** button.


> type=note
> While performing **BatchEdit** in FlexGrid, if you do sorting, filtering or paging in the grid, an update request is made to the server, which automatically updates the data in source database. Hence, ensure that the **DisableServerRead** property of [ItemSource](/componentone/api/mvc/online-mvc-core/dotnet-api/C1.AspNetCore.Mvc/C1.Web.Mvc.IItemsSource-1.html) is set to **True**.
> 
> On setting the property value to true, batch update can only be performed by explicitly calling the Commit method of CollectionView in the **Update** button as shown in the example below:

![](https://cdn.mescius.io/document-site-files/images/9b6a6cfe-b8e8-42e9-8a04-da6cb7762977/images/collectionviewbatchupdate.png)

The following code example demonstrates how to use **BatchEdit** property and [CollectionViewEditRequest](/componentone/api/mvc/online-mvc-core/dotnet-api/C1.AspNetCore.Mvc/C1.Web.Mvc.CollectionViewEditRequest-1.html) to enable updating of multiple records simultaneously in the FlexGrid:

#### In Code

**BatchUpdateController.cs**

```csharp
private C1NWindEntities db = new C1NWindEntities();
// GET: Home
public ActionResult Index()
{
    return View(db);
}
public ActionResult GridBatchEdit([C1JsonRequest]CollectionViewBatchEditRequest<Category> requestData)
{
    return this.C1Json(CollectionViewHelper.BatchEdit(requestData, batchData =>
    {
        var itemresults = new List<CollectionViewItemResult<Category>>();
        string error = string.Empty;
        bool success = true;
        try
        {
            if (batchData.ItemsCreated != null)
            {
                batchData.ItemsCreated.ToList().ForEach(st =>
                {
                    db.Categories.Add(st);
                    itemresults.Add(new CollectionViewItemResult<Category>
                    {
                        Error = "",
                        Success = ModelState.IsValid,
                        Data = st
                    });
                });
            }
            if (batchData.ItemsDeleted != null)
            {
                batchData.ItemsDeleted.ToList().ForEach(category =>
                {
                    var fCategory = db.Categories.Find(category.CategoryID);
                    db.Categories.Remove(fCategory);
                    itemresults.Add(new CollectionViewItemResult<Category>
                    {
                        Error = "",
                        Success = ModelState.IsValid,
                        Data = category
                    });
                });
            }
            if (batchData.ItemsUpdated != null)
            {
                batchData.ItemsUpdated.ToList().ForEach(category =>
                {
                    db.Entry(category).State = EntityState.Modified;
                    itemresults.Add(new CollectionViewItemResult<Category>
                    {
                        Error = "",
                        Success = ModelState.IsValid,
                        Data = category
                    });
                });
            }
            db.SaveChanges();
        }
        catch (DbEntityValidationException e)
        {
            error = string.Join(",", e.EntityValidationErrors.SelectMany(i => i.ValidationErrors).Select(i => i.ErrorMessage));
            success = false;
        }
        catch (Exception e)
        {
            error = e.Message;
            success = false;
        }
        return new CollectionViewResponse<Category>
        {
            Error = error,
            Success = success,
            OperatedItemResults = itemresults
        };
    }, () => db.Categories.ToList()));
}
```

In this example, the **BatchEdit** request is assigned to BatchEdit property of FlexGrid's [ItemsSource](/componentone/api/mvc/online-mvc-core/dotnet-api/C1.AspNetCore.Mvc/C1.Web.Mvc.IItemsSource-1.html).

**BatchUpdate.cshtml**

### ASP.NET Core

```dotnet
<input type="button" value="Update" class="btn" onclick="batchUpdate()" />
<c1-flex-grid id="fGBECView" auto-generate-columns="false" allow-add-new="true" allow-delete="true" class="grid">
    <c1-items-source batch-edit="@Url.Action("GridBatchEdit")" disable-server-read="true"></c1-items-source>
    <c1-flex-grid-column binding="CategoryID"></c1-flex-grid-column>
    <c1-flex-grid-column binding="CategoryName"></c1-flex-grid-column>
    <c1-flex-grid-column binding="Description" width="500"></c1-flex-grid-column>
</c1-flex-grid>
```

### JS

```javascript
<script>
    //Batch Edit
    function batchUpdate() {
        var batchEditGrid = wijmo.Control.getControl('#fGBECView'),
        cv = batchEditGrid.collectionView;
        cv.commit();
    };
</script>
```