public ActionResult EditOrder(EachOrderModel editModel)//編輯訂單 { bool isExist = false; string account = ""; account = Session["account"].ToString(); string ADmeal = ""; string Meal = ""; string date = editModel.Eachday; if (editModel.ADmeal == "0") { ADmeal = "午餐"; } if (editModel.ADmeal == "1") { ADmeal = "晚餐"; } if (editModel.Meal == "0") { Meal = "A餐"; } if (editModel.Meal == "1") { Meal = "B餐"; } Loginsql(); SqlCommand cmd = new SqlCommand//檢查日期是否已存在 { Connection = em, CommandText = "SELECT COUNT(*) FROM Orderday2 WHERE orderday = '" + editModel.Eachday + "' AND ADmeal ='" + ADmeal + "'" }; if (em.State == ConnectionState.Closed) { em.Open(); } using (SqlDataReader reader = cmd.ExecuteReader()) { reader.Read(); int i = reader.GetInt32(0); isExist = reader.GetInt32(0) > 0; em.Close(); } if (isExist == true) { TempData["dontrepeat"] = "不可以重複購買" + editModel.Eachday + "" + ADmeal + ",請重新修改"; return(RedirectToAction("Index")); } if (ModelState.IsValid) { string sql = "UPDATE Orderday2 SET meal='" + Meal + "',ADmeal='" + ADmeal + "' WHERE oid ='" + editModel.Oid + "'"; ExecuteSql(sql); return(RedirectToAction("Index")); } else { return(View(editModel)); } }
public ActionResult EachOrder(EachOrderModel EachModel) { int deal = 0; if (Session["account"] == null || string.IsNullOrWhiteSpace(Session["account"].ToString())) //如果還沒有登入帳號就直接顯示Login view { return(View("Login")); } else { account = Session["account"].ToString(); } DateTime today2 = DateTime.Now; //取得今天的日期 DateTime today = DateTime.Today; string std = string.Format("{0:yyyy/MM/dd}", today2); //將today2日期轉為字串std DateTime compareday = Convert.ToDateTime(EachModel.Eachday); //將Eachday轉為日期格式 DateTime end = Convert.ToDateTime(EachModel.Eachday + "上午 10:00:00"); //10點 String month = compareday.Month.ToString(); //抓Eachday的月份 string day = compareday.DayOfWeek.ToString(); if (day == "Sunday" || day == "Saturday") { ViewBag.dayoff = "假日不可以訂購"; return(View(EachModel)); } if (std == EachModel.Eachday) { if (today2 > end) { ViewBag.tenoff = "請在當日十點以前訂購"; return(View(EachModel)); } } if (today > compareday) { ViewBag.dayoff = "不可以訂購" + std + "以前的日期"; return(View(EachModel)); } bool isExist = false; Loginsql(); SqlCommand cmd = new SqlCommand//檢查日期是否已存在 { Connection = em, CommandText = "SELECT COUNT(*) FROM Orderday2 WHERE orderday = '" + EachModel.Eachday + "'AND account='" + account + "'" }; if (em.State == ConnectionState.Closed) { em.Open(); } using (SqlDataReader reader = cmd.ExecuteReader()) { reader.Read(); deal = reader.GetInt32(0); //將訂單數存到deal em.Close(); } if (deal >= 2) { ViewBag.deal2 = "此日期已訂購完畢"; return(View(EachModel)); } else if (deal == 1) {//如果有一筆資料,查是午餐還晚餐,並顯示您已訂購過 Loginsql(); cmd.CommandText = "SELECT COUNT(*) FROM Orderday2 WHERE orderday = '" + EachModel.Eachday + "'AND account='" + account + "'AND ADmeal ='午餐'"; using (SqlDataReader reader = cmd.ExecuteReader()) { reader.Read(); isExist = reader.GetInt32(0) > 0; //午餐訂單是否存在 em.Close(); } if (isExist == true && EachModel.ADmeal == "0") { ViewBag.ResultMessage = "您已訂購午餐,請重新輸入"; return(View(EachModel)); } else if (isExist == false && EachModel.ADmeal == "1") { ViewBag.ResultMessage = "您已訂購晚餐請重新輸入"; return(View(EachModel)); } } string ADmeal = ""; string Meal = ""; if (EachModel.ADmeal == "0") { ADmeal = "午餐"; } if (EachModel.ADmeal == "1") { ADmeal = "晚餐"; } if (EachModel.Meal == "0") { Meal = "A餐"; } if (EachModel.Meal == "1") { Meal = "B餐"; } string sql = "INSERT INTO Orderday2(account,orderday,meal,ADmeal,month)VALUES('" + account + "','" + EachModel.Eachday + "','" + Meal + "','" + ADmeal + "','" + month + "')"; ExecuteSql(sql); em.Close(); return(RedirectToAction("Index")); }