コード例 #1
0
        public ActionResult AddMealSet(MealSetViewModel model)
        {
            ViewBag.CheckRunTimes = "2";

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

            string updateBy = AccountInfo.GetUserName(Request);
            DateTime date = DateTime.Now;
            string mealSetName = model.MealSetName;
            string description = model.Description;
            bool canEatMore = model.CanEatMore;
            string imgPath = null;
            string savePath = null;

            MealSetTableAdapter mealSetAdapter = new MealSetTableAdapter();
            DataTable mealSetDT = mealSetAdapter.GetDataByName(mealSetName);

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

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

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

            try
            {

                string mealSetID = mealSetAdapter.InsertMealSetScalar(mealSetName, imgPath, description, canEatMore, date, updateBy, date).ToString();
                int id = int.Parse(mealSetID);
                XmlSync.SaveMealSetXml(id, mealSetName, canEatMore, date, updateBy, date, null);
                Log.ActivityLog("Insert into MealSet Table: MealSetName = " + mealSetName);
                Session["addMeslSet"] = "Thêm suất ăn thành công!";
            }
            catch (Exception ex)
            {
                Log.ErrorLog(ex.Message);
                Session["addMeslSet"] = "Thêm suất ăn thất bại!";
            }
            return RedirectToAction("AddMealSet", "MealSet");
        }
コード例 #2
0
        public ActionResult EditMealSet(EditMealSetModel model)
        {
            if (!ModelState.IsValid)
            {
                return View(model);
            }

            MealSetTableAdapter mealSetAdapter = new MealSetTableAdapter();

            string updateBy = AccountInfo.GetUserName(Request);
            DateTime date = DateTime.Now;
            int mealSetID = model.MealSetID;
            string mealSetName = model.MealSetName;
            string description = model.Description;
            bool canEatMore = model.CanEatMore;

            DataTable dt = mealSetAdapter.GetDataByMealSetID(mealSetID);

            string savePath = "\\Images\\MealSetImages\\" + mealSetName.Replace(" ", "_") + ".jpg";

            if (!StringExtensions.EqualsInsensitive(dt.Rows[0]["Name"].ToString(), mealSetName))
            {
                DataTable mealSetDT = mealSetAdapter.GetDataByName(mealSetName);

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

                savePath = "\\Images\\MealSetImages\\" + mealSetName.Replace(" ", "_") + ".jpg";

            }
            if (model.Image != null)
            {
                if (model.Image.Contains("Temp2"))
                {
                    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
            {
                mealSetAdapter.UpdateMealSet(mealSetName, savePath, description, canEatMore, updateBy, date, mealSetID);
                XmlSync.SaveMealSetXml(mealSetID, mealSetName, canEatMore, date, updateBy, date, null);
                Log.ActivityLog("Update to MealSet Table: MealSetID = " + mealSetID);
                Session["editMealSet"] = "Cập nhật thành công!";
            }
            catch (Exception ex)
            {
                Log.ErrorLog(ex.Message);
                Session["editMealSet"] = "Cập nhật thành công!";
            }

            return RedirectToAction("EditMealSet", "MealSet", new { @mealSetID = model.MealSetID});
        }