# Deleting

Delete a record from the database using server-side operation supported by the CollectionView. Learn more about deleting records using CollectionView in MVC documentaion.

## Content



This example shows how to define action in controller to delete rows from database by handling Edit request of **CollectionViewHelper**.

The following image shows how the FlexGrid appears after the [CollectionViewEditRequest](/componentone/api/mvc/online-mvc/dotnet-framework-api/C1.Web.Mvc/C1.Web.Mvc.CollectionViewEditRequest-1.html) is used in the controller.

The example uses **C1NWind** datasource, which was configured in the application in the [Quick Start](/componentone/docs/mvc/online-mvc/workwithcontrols/CollectionView/CollectionViewQuickStart). To **delete** a row, click the row header to select the row, and click **Delete** button on the keyboard. The selected row is deleted and the source database is updated accordingly.

![Showing records in FlexGrid](https://cdn.mescius.io/document-site-files/images/2b3ac322-100e-4637-958d-fb40dcda3f44/images/collectionviewdelete.png)

The following code demonstrates how to handle [CollectionViewEditRequest](/componentone/api/mvc/online-mvc/dotnet-framework-api/C1.Web.Mvc/C1.Web.Mvc.CollectionViewEditRequest-1.html) to enable deleting of a record in the FlexGrid:

#### In Code

**DeleteController.cs**

```csharp
//Define datasource for FlexGrid
private C1NWindEntities db = new C1NWindEntities();
public ActionResult Index()
{
    return View(db);
}
//Instantiate a JSON request
public ActionResult GridDeleteCategory([C1JsonRequest]CollectionViewEditRequest<Category> requestData)
{
    return Delete(requestData, db.Categories, item => item.CategoryID);
}
private ActionResult Delete<T>(CollectionViewEditRequest<T> requestData, DbSet<T> data, Func<T, object> getKey) where T : class
{
    return this.C1Json(CollectionViewHelper.Edit<T>(requestData, item =>
    {
        string error = string.Empty;
        bool success = true;
        try
        {
            var resultItem = data.Find(getKey(item));
            data.Remove(resultItem);
            db.SaveChanges();
        }
        catch (DbEntityValidationException e)
        {
            error = string.Join(",", e.EntityValidationErrors.Select(result =>
            {
                return string.Join(",", result.ValidationErrors.Select(err => err.ErrorMessage));
            }));
            success = false;
        }
        catch (Exception e)
        {
            error = e.Message;
            success = false;
        }
        return new CollectionViewItemResult<T>
        {
            Error = error,
            Success = success && ModelState.IsValid,
            Data = item
        };
    }, () => data.ToList<T>()));
}
```

In this example, the **Delete** action is assigned to the Delete property of FlexGrid's [ItemSource](/componentone/api/mvc/online-mvc/dotnet-framework-api/C1.Web.Mvc/C1.Web.Mvc.IItemsSource-1.html).

**Delete.cshtml**

```razor
@(Html.C1().FlexGrid().Id("fGDelCView").AutoGenerateColumns(false).IsReadOnly(true)
    .Columns(columns => columns
        .Add(c => c.Binding("CategoryID"))
        .Add(c => c.Binding("CategoryName"))
        .Add(c => c.Binding("Description").Width("*")))
    .AllowDelete(true)
    .Bind(
        ib => ib.Bind(Model.Categories)
        .Delete(Url.Action("GridDeleteCategory"))
    )
)
```