Пример #1
0
        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});
        }
Пример #2
0
        public ActionResult EditDish(string dishID)
        {
            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;

            try
            {
                int id = int.Parse(dishID);

                EditDishModel model = new EditDishModel();
                DishTableAdapter dishAdapter = new DishTableAdapter();
                DataTable dishDT = dishAdapter.GetDataByDishID(id);
                model.DishID = id;
                model.Dishname = dishDT.Rows[0]["Name"].ToString();
                model.DishTypeID = (int)dishDT.Rows[0]["DishTypeID"];
                model.Description = dishDT.Rows[0]["Description"].ToString();
                model.Image = dishDT.Rows[0]["Image"].ToString();
                return View(model);
            }
            catch (Exception ex)
            {
                Log.ErrorLog(ex.Message);
                return RedirectToAction("Error", "Error");
            }
        }