public ActionResult AddOrder(List <OrderDetail> arr, Order1 data2) { int orderID = 0; data2.CreateDate = DateTime.Now; db.Order1.Add(data2); db.SaveChanges(); orderID = data2.OrderID; foreach (OrderDetail item in arr) { item.OrderID = orderID; } db.OrderDetails.AddRange(arr.AsEnumerable()); db.SaveChanges(); int? total = 0; string content = System.IO.File.ReadAllText(Server.MapPath("~/Areas/client/template/email.html")); foreach (var item in arr) { content = content.Replace("{{NameProduct}}", item.Name); content = content.Replace("{{soluong}}", item.Quantity.ToString()); string.Format("{0:#,##0}", total += item.Quantity * item.Price); } string shipEmail = data2.ShipEmail; string recipient = shipEmail; content = content.Replace("{{CustomerName}}", data2.ShipName); content = content.Replace("{{Phone}}", data2.ShipMobile); content = content.Replace("{{Email}}", data2.ShipEmail); content = content.Replace("{{Address}}", data2.ShipAddress); content = content.Replace("{{Total}}", total.ToString()); WebMail.SmtpServer = "smtp.gmail.com"; WebMail.SmtpPort = 587; WebMail.SmtpUseDefaultCredentials = true; WebMail.EnableSsl = true; WebMail.UserName = "******"; WebMail.Password = "******"; WebMail.Send(to: recipient, subject: "Đơn Hàng Từ SWE", body: content); return(RedirectToAction("Index")); }
public JsonResult Add(Sanpham1 sanpham) { try { if (ModelState.IsValid) { //add vào db db.Sanpham1.Add(sanpham); db.SaveChanges(); return(Json(new { StatusCode = 200 }, JsonRequestBehavior.AllowGet)); } else { //Tên input, lỗi IEnumerable <ModelError> allErrors = ModelState.Values.SelectMany(v => v.Errors); return(Json(new { StatusCode = 500, Message = allErrors.FirstOrDefault().ErrorMessage }, JsonRequestBehavior.AllowGet)); } } catch { return(Json(new { StatusCode = 505, Message = "Lỗi Thêm Mới" }, JsonRequestBehavior.AllowGet)); } }