public ActionResult Create(Product product) { if (ModelState.IsValid) { db.Products.Add(product); db.SaveChanges(); if (product.ImageFile != null) { var folder = "~/Content/Products"; var file = string.Format("{0}.jpg", product.ProductId); var response = FilesHelper.UploadPhoto(product.ImageFile, folder, file); if (response) { var pic = string.Format("{0}/{1}.jpg", folder, product.ProductId); product.Image = pic; db.Entry(product).State = EntityState.Modified; db.SaveChanges(); } } return(RedirectToAction("Index")); } ViewBag.CategoryId = new SelectList(db.Categories, "CategoryId", "Description", product.CategoryId); ViewBag.CompanyId = new SelectList(db.Companies, "CompanyId", "Name", product.CompanyId); ViewBag.TaxId = new SelectList(db.Taxes, "TaxId", "Description", product.TaxId); return(View(product)); }
public ActionResult Create([Bind(Include = "CityId,Name,DepartmentId")] City city) { if (ModelState.IsValid) { db.Cities.Add(city); try { db.SaveChanges(); return(RedirectToAction("Index")); } catch (Exception ex) { if (ex.InnerException != null && ex.InnerException.InnerException != null && ex.InnerException.InnerException.Message.Contains("_Index")) { ModelState.AddModelError(string.Empty, "The are record we the same name in department"); } else { ModelState.AddModelError(string.Empty, ex.Message); } } } ViewBag.DepartmentId = new SelectList(CombosHelper.GetDepartment(), "DepartmentId", "Name", city.DepartmentId); return(View(city)); }
public IHttpActionResult PutUser(int id, User user) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != user.UserId) { return(BadRequest()); } db.Entry(user).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!UserExists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }
public ActionResult Create([Bind(Include = "DepartmentId,Name")] Department department) { if (ModelState.IsValid) { db.Departments.Add(department); try { db.SaveChanges(); return(RedirectToAction("Index")); } catch (Exception ex) { if (ex.InnerException != null && ex.InnerException.InnerException != null && ex.InnerException.InnerException.Message.Contains("_Index")) { ModelState.AddModelError(string.Empty, "The are record we the same name"); } else { ModelState.AddModelError(string.Empty, ex.Message); } } } return(View(department)); }
public ActionResult Create(User user) { if (ModelState.IsValid) { db.Users.Add(user); db.SaveChanges(); UsersHelper.CreateUserASP(user.UserName, "User"); if (user.PhotoFile != null) { var folder = "~/Content/Users"; var file = string.Format("{0}.jpg", user.UserId); var response = FilesHelper.UploadPhoto(user.PhotoFile, folder, file); if (response) { var pic = string.Format("{0}/{1}.jpg", folder, user.UserId); user.Photo = pic; db.Entry(user).State = EntityState.Modified; db.SaveChanges(); } } return(RedirectToAction("Index")); } ViewBag.CityId = new SelectList(CombosHelper.GetCities(user.DepartmentId), "CityId", "Name", user.CityId); ViewBag.CompanyId = new SelectList(CombosHelper.GetCompanies(), "CompanyId", "Name", user.CompanyId); ViewBag.DepartmentId = new SelectList(CombosHelper.GetDepartment(), "DepartmentId", "Name", user.DepartmentId); return(View(user)); }
public ActionResult Create(Company company) { if (ModelState.IsValid) { db.Companies.Add(company); db.SaveChanges(); if (company.LogoFile != null) { var folder = "~/Content/Logos"; var file = string.Format("{0}.jpg", company.CompanyId); var response = FilesHelper.UploadPhoto(company.LogoFile, folder, file); if (response) { var pic = string.Format("{0}/{1}.jpg", folder, company.CompanyId); company.Logo = pic; db.Entry(company).State = EntityState.Modified; db.SaveChanges(); } } return(RedirectToAction("Index")); } ViewBag.CityId = new SelectList(CombosHelper.GetCities(company.DepartmentId), "CityId", "Name", company.CityId); ViewBag.DepartmentId = new SelectList(CombosHelper.GetDepartment(), "DepartmentId", "Name", company.DepartmentId); return(View(company)); }
public ActionResult Create([Bind(Include = "StateId,Description")] State state) { if (ModelState.IsValid) { db.States.Add(state); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(state)); }
public ActionResult Create(Category category) { if (ModelState.IsValid) { db.Categories.Add(category); db.SaveChanges(); return(RedirectToAction("Index")); } //ViewBag.CompanyId = new SelectList(db.Companies, "CompanyId", "Name", category.CompanyId); return(View(category)); }
public ActionResult Create(Tax tax) { if (ModelState.IsValid) { db.Taxes.Add(tax); db.SaveChanges(); return(RedirectToAction("Index")); } //ViewBag.CompanyId = new SelectList(db.Companies, "CompanyId", "Name", tax.CompanyId); return(View(tax)); }
public static Response NewOrder(NewOrderView view, string name) { using (var transaction = db.Database.BeginTransaction()) { try { var user = db.Users.Where(c => c.UserName == name).FirstOrDefault(); var order = new Order { CompanyId = user.CompanyId, CustomerId = view.CustomerId, Date = view.Date, Remarks = view.Remarks, StateId = DBHelper.GetState("Created", db), }; db.Orders.Add(order); db.SaveChanges(); var details = db.OrderDetailTmps.Where(odt => odt.UserName == name).ToList(); foreach (var item in details) { var orderdetail = new OrderDetail { Description = item.Description, OrderId = order.OrderId, Price = item.Price, ProductId = item.ProductId, Quantity = item.Quantity, TaxRate = item.TaxRate, }; db.OrderDetails.Add(orderdetail); db.OrderDetailTmps.Remove(item); } db.SaveChanges(); transaction.Commit(); return(new Response { Succeeded = true, }); } catch (Exception ex) { transaction.Rollback(); return(new Response { Succeeded = false, Message = ex.Message, }); } } }
public static Response SaveChanges(KissiContext db) { try { db.SaveChanges(); return(new Response { Succeeded = true, }); } catch (Exception ex) { var response = new Response { Succeeded = false, }; if (ex.InnerException != null && ex.InnerException.InnerException != null && ex.InnerException.InnerException.Message.Contains("_Index")) { response.Message = "There is a record with the same value"; } else if (ex.InnerException != null && ex.InnerException.InnerException != null && ex.InnerException.InnerException.Message.Contains("REFERENCE")) { response.Message = "The record can't be delete because it has related records"; } else { response.Message = ex.Message; } return(response); } }
public ActionResult DeleteProduct(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } var ordetailtmp = db.OrderDetailTmps.Where(odt => odt.UserName == User.Identity.Name && odt.ProductId == id).FirstOrDefault(); if (ordetailtmp == null) { return(HttpNotFound()); } db.OrderDetailTmps.Remove(ordetailtmp); db.SaveChanges(); return(RedirectToAction("Create")); // return View(order); }
public ActionResult DeleteConfirmed(int id) { Warehouse warehouse = db.Warehouses.Find(id); db.Warehouses.Remove(warehouse); db.SaveChanges(); return(RedirectToAction("Index")); }
public ActionResult Create(Customer customer) { if (ModelState.IsValid) { using (var transaccion = db.Database.BeginTransaction()) { try { db.Customers.Add(customer); db.SaveChanges(); var user = db.Users.Where(u => u.UserName == User.Identity.Name).FirstOrDefault(); var companyCustomer = new CompanyCustomer { CompanyId = user.CompanyId, CustomerId = customer.CustomerId, }; db.CompanyCustomers.Add(companyCustomer); db.SaveChanges(); UsersHelper.CreateUserASP(customer.UserName, "Customer"); transaccion.Commit(); return(RedirectToAction("Index")); } catch (Exception ex) { transaccion.Rollback(); ModelState.AddModelError(string.Empty, ex.Message); } } } ViewBag.CityId = new SelectList(CombosHelper.GetCities(customer.DepartmentId), "CityId", "Name", customer.CityId); //ViewBag.CompanyId = new SelectList(db.Companies, "CompanyId", "Name", customer.CompanyId); ViewBag.DepartmentId = new SelectList(CombosHelper.GetDepartment(), "DepartmentId", "Name", customer.DepartmentId); return(View(customer)); }
public static int GetState(string description, KissiContext db) { var state = db.States.Where(c => c.Description == description).FirstOrDefault(); if (state == null) { state = new State { Description = description, }; db.States.Add(state); db.SaveChanges(); } return(state.StateId); }