# Edit Grids with Entity Framework Code First

## Environment

<table>
 <tr>
  <td>Product</td>
  <td>Telerik UI for ASP.NET MVC Grid</td>
 </tr>
 <tr>
  <td>Product version</td>
  <td>2025.1.227</td>
 </tr>
</table>

## Description

How can I edit Grids with Entity Framework Code First?

## Solution

You can achieve this requirement using the following implementation:

1. Define a Grid that binds to remote data:

   ```Razor HtmlHelper
   @(Html.Kendo().Grid<Telerik.Examples.Mvc.Areas.GridEditingEFCodeFirst.Models.Customer>()
       .Name("Customers")
       .ToolBar(tb => tb.Create())
       .Pageable()
       .DataSource(dataSource => dataSource
           .Ajax()
           .Model(model => model.Id(c => c.CustomerID))
           .Read("GetCustomers", "Home")
           .Update("UpdateCustomer", "Home")
           .Create("InsertCustomer", "Home")
           .Destroy("DeleteCustomer", "Home")
       )
       .Columns(cols =>
       {
           cols.Bound(c => c.Name).Width(300);
           cols.Bound(c => c.Phone);
           cols.Bound(c => c.Email);
           cols.Command(cmd =>
           {
               cmd.Edit();
               cmd.Destroy();
           });
       })
   )
   ```

2. Implement the Action methods for the CRUD operations:

   ```C#
   public ActionResult GetCustomers([DataSourceRequest] DataSourceRequest request)
   {
       using (var dbContext = new CustomerContext())
       {
           return Json(dbContext.Customers.ToDataSourceResult(request,
                   c => new CustomerViewModel
                   {
                       CustomerID = c.CustomerID,
                       Name = c.Name,
                       Email = c.Email,
                       Phone = c.Phone
                   }));
       }
   }

   public ActionResult UpdateCustomer([DataSourceRequest] DataSourceRequest request, Customer customer)
   {
       using (var dbContext = new CustomerContext())
       {
           var customerToUpdate = dbContext.Customers.First(cust => cust.CustomerID == customer.CustomerID);

           TryUpdateModel(customerToUpdate);

           dbContext.SaveChanges();

           return Json(ModelState.ToDataSourceResult());
       }
   }

   public ActionResult InsertCustomer([DataSourceRequest] DataSourceRequest request, Customer customerToAdd)
   {
       using (var dbContext = new CustomerContext())
       {
           if (ModelState.IsValid)
           {
               dbContext.Customers.Add(customerToAdd);
               dbContext.SaveChanges();
           }

           return Json(new[] { customerToAdd }.ToDataSourceResult(request));
       }
   }

   public ActionResult DeleteCustomer([DataSourceRequest] DataSourceRequest request, Customer customer)
   {
       using (var dbContext = new CustomerContext())
       {
           var customerToDelete = dbContext.Customers.First(cust => cust.CustomerID == customer.CustomerID);

           if (customerToDelete != null)
           {
               dbContext.Customers.Remove(customerToDelete);
               dbContext.SaveChanges();
           }

           return Json(new[] { customerToDelete }.ToDataSourceResult(request));
       }
   }
   ```

To review the complete implementation, refer to the [project on how to implement Grid's CRUD data operations using Entity Framework Code First](https://github.com/telerik/ui-for-aspnet-mvc-examples/tree/master/Telerik.Examples.Mvc/Telerik.Examples.Mvc/Areas/GridEditingEFCodeFirst).

## More ASP.NET MVC Grid Resources

* [ASP.NET MVC Grid Documentation](https://www.telerik.com/aspnet-mvc/documentation/html-helpers/data-management/grid/overview)
* [ASP.NET MVC Grid Demos](https://demos.telerik.com/aspnet-mvc/grid/index)
* [ASP.NET MVC Grid Product Page](https://www.telerik.com/aspnet-mvc/grid)
* [Telerik UI for ASP.NET MVC Video Onboarding Course (Free for trial users and license holders)](https://www.telerik.com/aspnet-mvc/documentation/getting-started/video-onboarding)
* [Telerik UI for ASP.NET MVC Forums](https://www.telerik.com/forums/aspnet-mvc)

## See Also

* [Client-Side API Reference of the Grid for ASP.NET MVC](https://docs.telerik.com/kendo-ui/api/ui/grid)
* [Server-Side API Reference of the Grid for ASP.NET MVC](https://docs.telerik.com/aspnet-mvc/api/grid)
* [Telerik UI for ASP.NET MVC Knowledge Base](https://docs.telerik.com/aspnet-mvc/knowledge-base)
