public ActionResult Create(OrderModel order) { try { if (ModelState.IsValid) { //DbContext.Orders.Add(order); //if (DbContext.SaveChanges() > 0) // return Redirect(null); //ModelState.AddModelError("", "Đã có lỗi xảy ra. Vui lòng thử lại sau."); } } catch (Exception ex) { ModelState.AddModelError("", ex.Message); ex.Write(LogPath); } InitFormData(order); return View(order); }
public ActionResult Create() { var model = new OrderModel() { SaleDate = DateTime.Now.ToString("dd/MM/yyyy") }; InitFormData(model); return View(model); }
private void InitFormData(OrderModel order) { var customers = DbContext.Customers.Select(x => new { CustomerId = x.CustomerId, FullName = x.CustomerId + " - " + x.FullName }).ToList(); order.Customers = new SelectList(customers, "CustomerId", "FullName", order.CustomerId); var methods = new[] { new { value = "1", text = "Thanh toán trực tiếp"}, new { value = "2", text = "Thanh toán nợ"} }; order.MethodofPayments = new SelectList(methods, "value", "text", order.MethodofPayment); var product = DbContext.Products.Select(x => new { ProductId = x.ProductId + "-" + x.PriceA + "-" + x.PriceB, ProductText = x.ProductId + " - " + x.ProductName }).ToList(); order.Products = new SelectList(product, "ProductId", "ProductText", null); }