Skip to main content Skip to footer

How to Enable Asynchronous Editing in FlexGrid for ASP.NET MVC

Users that may need to make asynchronous calls to a web service for their data collection can use this server-side logic to efficiently handle multiple data modifications (create, update, delete) from their client-side FlexGrid

The C# GridBatchEdit action serves as the single endpoint that receives a bundled request from the client's CollectionView. It minimizes network latency by processing all changes in one asynchronous transaction, instead of sending a separate call for every single grid edit.

public ActionResult GridBatchEdit([C1JsonRequest] CollectionViewBatchEditRequest<Person> requestData)
{
    return this.C1Json(CollectionViewHelper.BatchEdit(requestData, batchData =>
    {
        var itemresults = new List<CollectionViewItemResult<Person>>();
        string error = string.Empty;
        bool success = true;
        try
        {
            if (batchData.ItemsCreated != null)
            {
                batchData.ItemsCreated.ToList().ForEach(st =>
                {
                    //_db.Categories.Add(st);
                    Persons.Add(st);
                    itemresults.Add(new CollectionViewItemResult<Person>
                    {
                        Error = "",
                        Success = success,
                        Data = st
                    });
                });
            }
            if (batchData.ItemsDeleted != null)
            {
                batchData.ItemsDeleted.ToList().ForEach(category =>
                {
                    //_db.Categories.Remove(category);
                    Persons.Remove(category);
                    itemresults.Add(new CollectionViewItemResult<Person>
                    {
                        Error = "",
                        Success = success,
                        Data = category
                    });
                });
            }
            if (batchData.ItemsUpdated != null)
            {
                batchData.ItemsUpdated.ToList().ForEach(category =>
                {
                    //_db.Entry(category).State = EntityState.Modified;
                    var index = Persons.FindIndex(x => x.ID == category.ID);
                    Persons[index] = category;
                    itemresults.Add(new CollectionViewItemResult<Person>
                    {
                        Error = "",
                        Success = success,
                        Data = category
                    });
                });
            }
        }
        catch (Exception e)
        {
            //error = GetExceptionMessage(e);
            error = e.Message;
            success = false;
        }

        return new CollectionViewResponse<Person>
        {
            Error = error,
            Success = success,
            OperatedItemResults = itemresults
        };
    }, () => Persons));
}