public ActionResult Customers_Destroy([DataSourceRequest]DataSourceRequest request, CustomerViewModel customer) { if (ModelState.IsValid) { using (var northwind = new NorthwindEntities()) { var entity = new Customer { CustomerID = customer.CustomerID, CompanyName = customer.CompanyName, Country = customer.Country, City = customer.City, ContactName = customer.ContactName, Phone = customer.Phone, }; northwind.Customers.Attach(entity); northwind.Customers.Remove(entity); northwind.SaveChanges(); } } return Json(new[] { customer }.ToDataSourceResult(request, ModelState)); }
public ActionResult Customers_Create([DataSourceRequest]DataSourceRequest request, CustomerViewModel customer) { if (ModelState.IsValid) { using (var northwind = new NorthwindEntities()) { var entity = new Customer { CustomerID = Guid.NewGuid().ToString().Substring(0,5), CompanyName = customer.CompanyName, Country = customer.Country, City = customer.City, ContactName = customer.ContactName, Phone = customer.Phone, }; northwind.Customers.Add(entity); northwind.SaveChanges(); customer.CustomerID = entity.CustomerID; } } return Json(new[] { customer }.ToDataSourceResult(request, ModelState)); }