public ActionResult Save(Product p) { try { if (p.ProductID != 0) { db.Products.Attach(p); db.Entry(p).State = EntityState.Modified; } if (p.ProductID == 0) { db.Products.Add(p); } db.SaveChanges(); ViewBag.produto = Products(); ViewBag.Categorias = Categories(); ViewBag.Products = Products(); } catch (Exception ex) { ex = ErrorException(ex); return(View("Erro", ex)); } ViewBag.Message = "O Registro Foi Salvo com Sucesso"; return(View("New", new Product())); }
public ActionResult SaveJson(Supplier e) { var resposta = new RespostaHtml { success = true }; try { if (e.SupplierID == 0) { db.Suppliers.Add(e); } else { db.Suppliers.Attach(e); db.Entry(e).State = EntityState.Modified; } db.SaveChanges(); resposta.Data = e; } catch (Exception ex) { ex = ErrorException(ex); resposta.success = false; resposta.message = ex.Message; } return(Json(resposta, JsonRequestBehavior.DenyGet)); }
public ActionResult Delete(int EmployeeId) { ViewBag.Message = "O Registro foi excluido com sucesso"; var empregado = (from e in db.Employees where e.EmployeeID == EmployeeId select e).FirstOrDefault(); if (empregado == null) { var ex = new Exception("Registro Não encontrado!"); return(View("Erro", ex)); } try { db.Employees.Remove(empregado); db.SaveChanges(); } catch (Exception ex) { return(View("Erro", ErrorException(ex))); } ViewBag.Chefes = ObterChefes(); return(View("New", new Employee())); }
public ActionResult SaveJson(Supplier s) { var resposta = new RespostaHtml { success = true }; try { if (s.SupplierID == 0) { //Como eu salvo um registro no Banco de Dados? db.Suppliers.Add(s); } else { db.Suppliers.Attach(s); db.Entry(s).State = EntityState.Modified; } db.SaveChanges(); resposta.Data = s; } catch (Exception ex) { ex = ErrorException(ex); resposta.success = false; resposta.message = ex.Message; return(Json(resposta, JsonRequestBehavior.DenyGet)); } return(Json(resposta, JsonRequestBehavior.DenyGet)); }
public ActionResult Save(Employee e) { try { if (e.EmployeeID != 0) { db.Employees.Attach(e); db.Entry(e).State = EntityState.Modified; } if (e.EmployeeID == 0) { db.Employees.Add(e); } db.SaveChanges(); var emps = (from emp in db.Employees select emp).ToList(); ViewBag.Empregados = emps; } catch (Exception ex) { ex = ErrorException(ex); return(View("Erro", ex)); } ViewBag.Message = "O Registro Foi Salvo com Sucesso"; return(View("New", new Employee())); }
public IHttpActionResult PutCustomer(string id, Customer customer) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != customer.CustomerID) { return(BadRequest()); } db.Entry(customer).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!CustomerExists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }
public ActionResult Edit(Customer customer) { try { db.Customers.Attach(customer); db.Entry(customer).State = System.Data.Entity.EntityState.Modified; db.SaveChanges(); } catch (Exception ex) { ex = ErrorException(ex); return(View("Erro", ex)); } ViewBag.Message = "O registro foi alterado com sucesso"; return(View("Details", customer)); }
// PUT: api/Category/5 public object Put([FromBody] CategoryViewModel value) // update gibi nullable yaptık, object yaptık vs gibi değişiklikler { using (NorthWind db = new NorthWind()) { var category = db.Categories.Find(value.CategoryID); if (category == null) { return(new { message = "Kategori bulunamadı" }); } try { category.CategoryName = value.CategoryName; category.Description = value.Description; category.Picture = value.Picture; db.SaveChanges(); return(new { message = "Kategori güncelleme işlemi başarılı" }); } catch (Exception ex) { return(new { message = "Kategori güncelleme işleminde bir hata oluştu", detail = ex.Message }); } } }
// POST: api/Category public object Post([FromBody] CategoryViewModel value) //insert gibi { using (NorthWind db = new NorthWind()) { try { db.Categories.Add(new Category() { CategoryName = value.CategoryName, Description = value.Description, Picture = value.Picture }); db.SaveChanges(); return(new { message = $"{value.CategoryName} isimli kategori ekleme başarılı" }); } catch (Exception ex) { return(new { message = $"Kategori ekleme işlemi sırasında bir hata oluştu", detail = ex.Message }); } } }
// DELETE: api/Category/5 public object Delete(int?id) // delete de delete gibi, nullable yaptık, object yaptık vs gibi değişiklikler var { using (NorthWind db = new NorthWind()) { var category = db.Categories.Find(id); if (category == null) { return(new { message = "Silinecek kategori bulunamadı" }); } try { db.Categories.Remove(category); db.SaveChanges(); return(new { message = "Kategori Sİlme işlemi başarılı" }); } catch (Exception ex) { return(new { message = "Kategori silme işleminde bir hata oluştu", details = ex.Message }); } } }
public ActionResult Delete(int EmployeeID) { var db = new NorthWind(); var employee = (from emp in db.Employees where emp.EmployeeID == EmployeeID select emp).FirstOrDefault(); if (employee == null) { var ex = new Exception("Registro não encontrado."); return(View("Erro", ex)); } else { db.Employees.Remove(employee); try { db.SaveChanges(); } catch (Exception ex) { ex = ErrorException(ex); return(View("Erro", ex)); } } var empregados = (from emp in db.Employees select emp).ToList(); ViewBag.Empregados = empregados; ViewBag.Message = "O registro foi excluído com sucesso!"; return(View("New", new Employee())); }
public ActionResult Delete(string ID) { var db = new NorthWind(); var customer = (from c in db.Customers where c.CustomerID == ID select c).FirstOrDefault(); if (customer == null) { var ex = new Exception("Registro não encontrado."); return(View("Erro", ex)); } else { db.Customers.Remove(customer); try { db.SaveChanges(); } catch (Exception ex) { return(View("Erro", ErrorException(ex))); } } var customers = (from c in db.Customers select c).ToList(); ViewBag.Customers = customers; ViewBag.Message = "O registro foi excluído com sucesso!"; return(View("Details", customer)); }
public ActionResult Save(Employee e) { try { //Usar nossa classe de banco de dados. //Instanciando a base. var db = new NorthWind(); if (e.Adventista == null) { e.Adventista = false; } //ou e.Adventista = e.Adventista == null ? false : true; if (e.EmployeeID == 0) { //Como eu salvo um registro no Banco de Dados? db.Employees.Add(e); } else { db.Employees.Attach(e); db.Entry(e).State = EntityState.Modified; } db.SaveChanges(); var boss = (from emp in db.Employees select emp).ToList(); ViewBag.Empregados = boss; } catch (Exception ex) { ex = ErrorException(ex); return(View("Erro", ex)); } ViewBag.Message = "O registro foi incluido com sucesso!"; return(View("New", new Employee())); }
public ActionResult Edit(Customer customer) { try { //Como eu salvo um registro no Banco de Dados? db.Customers.Add(customer); db.Entry(customer).State = EntityState.Modified; db.SaveChanges(); } catch (Exception ex) { ex = ErrorException(ex); return(View("Erro", ex)); } ViewBag.Message = "O registro foi incluido com sucesso!"; return(View("Details", customer)); }
public ActionResult SalvarDados(Order order) { try { db.Orders.Add(order); db.SaveChanges(); return(Json(order, JsonRequestBehavior.AllowGet)); } catch (Exception ex) { return(Json(ErrorException(ex), JsonRequestBehavior.AllowGet)); } }
public ActionResult Delete(int ProductID) { var db = new NorthWind(); var product = (from emp in db.Products where emp.ProductID == ProductID select emp).FirstOrDefault(); if (product == null) { var ex = new Exception("Registro não encontrado."); return(View("Erro", ex)); } else { db.Products.Remove(product); try { db.SaveChanges(); } catch (Exception ex) { ex = ErrorException(ex); return(View("Erro", ex)); } } var produtos = (from emp in db.Products select emp).ToList(); var sup = (from emp in db.Suppliers select emp).ToList(); var cat = (from emp in db.Categories select emp).ToList(); ViewBag.Categorias = cat; ViewBag.Fornecedores = sup; ViewBag.Produtos = produtos; ViewBag.Message = "O registro foi excluído com sucesso!"; return(View("New", new Product())); }
public ActionResult Save(Product p) { try { var db = new NorthWind(); if (p.ProductID == 0) { db.Products.Add(p); } else { db.Products.Attach(p); db.Entry(p).State = EntityState.Modified; } db.SaveChanges(); var prdts = (from emp in db.Employees select emp).ToList(); var sup = (from emp in db.Suppliers select emp).ToList(); var cat = (from emp in db.Categories select emp).ToList(); ViewBag.Categorias = cat; ViewBag.Fornecedores = sup; ViewBag.Produtos = prdts; } catch (Exception ex) { ex = ErrorException(ex); return(View("Erro", ex)); } ViewBag.Message = "O registro foi incluido com sucesso!"; return(View("New", new Product())); }
public ActionResult SaveJson(Employee e) { var resposta = new RespostaHtml { success = true }; try { //Usar nossa classe de banco de dados. //Instanciando a base. var db = new NorthWind(); if (e.Adventista == null) { e.Adventista = false; } //ou e.Adventista = e.Adventista == null ? false : true; if (e.EmployeeID == 0) { //Como eu salvo um registro no Banco de Dados? db.Employees.Add(e); } else { db.Employees.Attach(e); db.Entry(e).State = EntityState.Modified; } db.SaveChanges(); resposta.Data = e; } catch (Exception ex) { ex = ErrorException(ex); resposta.success = false; resposta.message = ex.Message; return(Json(resposta, JsonRequestBehavior.DenyGet)); } return(Json(resposta, JsonRequestBehavior.DenyGet)); }
public ActionResult DeleteJson(int EmployeeID) { var resposta = new RespostaHtml { success = true }; var db = new NorthWind(); var employee = (from emp in db.Employees where emp.EmployeeID == EmployeeID select emp).FirstOrDefault(); if (employee == null) { var ex = new Exception("Registro não encontrado"); resposta.success = false; resposta.message = ex.Message; return(Json(resposta, JsonRequestBehavior.DenyGet)); } else { db.Employees.Remove(employee); try { db.SaveChanges(); resposta.Data = employee; } catch (Exception ex) { ex = ErrorException(ex); resposta.success = false; resposta.message = ex.Message; return(Json(resposta, JsonRequestBehavior.DenyGet)); } } var empregados = (from emp in db.Employees select emp).ToList(); return(Json(resposta, JsonRequestBehavior.DenyGet)); }