コード例 #1
0
ファイル: DishController.cs プロジェクト: Grania/ctm-project
        public ActionResult AddDish(DishViewModel model)
        {
            DishTypeTableAdapter dishTypeAdapter = new DishTypeTableAdapter();
            DataTable dishTypeDT = dishTypeAdapter.GetData();

            List<SelectListItem> items = new List<SelectListItem>();
            foreach (DataRow row in dishTypeDT.Rows)
            {
                items.Add(new SelectListItem { Text = row["TypeName"].ToString(), Value = row["DishTypeID"].ToString() });
            }
            ViewData["DishType"] = items;
            ViewBag.CheckRunTimes = "2";

            if (!ModelState.IsValid)
            {
                return View(model);
            }

            string updateBy = AccountInfo.GetUserName(Request);
            DateTime date = DateTime.Now;
            string dishName = model.Dishname;
            int dishTypeID = model.DishTypeID;
            string description = model.Description;
            string imgPath = null;
            string savePath = null;

            DishTableAdapter dishAdapter = new DishTableAdapter();
            DataTable dishDT = dishAdapter.GetByName(dishName);

            foreach (DataRow row in dishDT.Rows)
            {
                if (StringExtensions.EqualsInsensitive(row["Name"].ToString(), dishName))
                {
                    ModelState.AddModelError("", "Tên món ăn đã tồn tại.");
                    return View(model);
                }
            }

            if (!string.IsNullOrEmpty(model.Image))
            {
                savePath = _sourcePath + dishName.Replace(" ", "_") + ".jpg";
                var sourcePath = AppDomain.CurrentDomain.BaseDirectory + model.Image;

                System.IO.File.Move(sourcePath, savePath);
                imgPath = "\\Images\\DishImages\\" + dishName.Replace(" ", "_") + ".jpg";
            }

            try
            {

                dishAdapter.InsertDish(dishName, dishTypeID, description, imgPath, date, updateBy, date);
                Log.ActivityLog("Insert into Dish Table: DishName = " + dishName);
                Session["addDish"] = "Thêm món ăn thành công!";
            }
            catch (Exception ex)
            {
                Log.ErrorLog(ex.Message);
                Session["addDish"] = "Thêm món ăn thất bại!";
            }
            return RedirectToAction("AddDish", "Dish");
        }
コード例 #2
0
ファイル: DishController.cs プロジェクト: Grania/ctm-project
        public ActionResult EditDish(EditDishModel model)
        {
            DishTypeTableAdapter dishTypeAdapter = new DishTypeTableAdapter();
            DataTable dishTypeDT = dishTypeAdapter.GetData();

            List<SelectListItem> items = new List<SelectListItem>();
            foreach (DataRow row in dishTypeDT.Rows)
            {
                items.Add(new SelectListItem { Text = row["TypeName"].ToString(), Value = row["DishTypeID"].ToString() });
            }
            ViewData["DishType"] = items;

            if (!ModelState.IsValid)
            {
                return View(model);
            }

            DishTableAdapter dishAdapter = new DishTableAdapter();

            string updateBy = AccountInfo.GetUserName(Request);
            DateTime date = DateTime.Now;
            int dishID = model.DishID;
            string dishName = model.Dishname;
            int dishTypeID = model.DishTypeID;
            string description = model.Description;

            DataTable dt = dishAdapter.GetDataByDishID(dishID);

            string savePath = "\\Images\\DishImages\\" + dishName.Replace(" ", "_") + ".jpg";

            if (!StringExtensions.EqualsInsensitive(dt.Rows[0]["Name"].ToString(), dishName))
            {
                DataTable dishDT = dishAdapter.GetByName(dishName);

                foreach (DataRow row in dishDT.Rows)
                {
                    if (StringExtensions.EqualsInsensitive(row["Name"].ToString(), dishName))
                    {
                        ModelState.AddModelError("", "Tên món ăn đã tồn tại.");
                        return View(model);
                    }
                }

                savePath = "\\Images\\DishImages\\" + dishName.Replace(" ", "_") + ".jpg";

            }

            if (model.Image != null)
            {
                if (model.Image.Contains("Temp"))
                {
                    string oldImage = dt.Rows[0]["Image"].ToString();
                    var oldImagePath = AppDomain.CurrentDomain.BaseDirectory + oldImage;
                    System.IO.File.Delete(oldImagePath);
                }

                System.IO.File.Move(AppDomain.CurrentDomain.BaseDirectory + model.Image, AppDomain.CurrentDomain.BaseDirectory + savePath);
            }
            else
            {
                savePath = null;
            }

            try
            {
                dishAdapter.UpdateDish(dishName, dishTypeID, description, savePath, updateBy, date, dishID);
                Log.ActivityLog("Update to Dish Table: DishID = " + dishID);
                Session["editDish"] = "Cập nhật thành công!";
            }
            catch (Exception ex)
            {
                Log.ErrorLog(ex.Message);
                Session["editDish"] = "Cập nhật thất bại!";
            }

            return RedirectToAction("EditDish", "Dish", new { @dishID = model.DishID});
        }