コード例 #1
0
        public ActionResult Create(SlideViewModel model)
        {
            string path = "";
            try
            {
                if (FileHelpers.ValidImage(model.image) == false)
                {
                    ModelState.AddModelError("image", "Please choose either a GIF, JPG or PNG image.");
                }

                if (ModelState.IsValid)
                {
                    path = FileHelpers.UploadImage(model.image);
                    model.image_path = path;
                    model.image = null;
                    model.created_by = UserHelpers.GetUser().id;
                    model.updated_by = UserHelpers.GetUser().id;

                    SetRequestURL(APIURL.SLIDE_INSERT, Method.POST);
                    request.AddBody(model);

                    var response = rest.Execute(request);

                    if (response.StatusCode == HttpStatusCode.OK)
                    {
                        long _id = JsonConvert.DeserializeObject<long>(response.Content);
                        SetMessage(Message.SuccessfulCreate("Slide"), MESSAGE_TYPE.SUCCESS);

                        return RedirectToAction("Index");
                    }
                    else
                    {
                        FileHelpers.DeleteFile(model.image_path);
                        ModelState.AddModelError("", response.Content);
                    }
                }
            }
            catch (Exception ex)
            {
                FileHelpers.DeleteFile(model.image_path);
                ModelState.AddModelError("", ex.Message);
            }

            model.image = null;
            model.alt_text = null;

            SetRequestURL(APIURL.SLIDE_LIST, Method.GET);
            var res = rest.Execute(request);

            if (res.StatusCode == HttpStatusCode.OK)
            {
                SetTitle("Slider");
                List<SlideViewModel> m = JsonConvert.DeserializeObject<List<SlideViewModel>>(res.Content);

                ViewData["SlideCount"] = m.Count;
                ViewData["Slides"] = m;

                return View("Index", model);
            }
            else
            {
                SetTitle();
                return CustomMessage(res.Content);
            }
        }
コード例 #2
0
        public ActionResult Edit(SlideViewModel model)
        {
            string path = "";
            try
            {
                if (model.image != null)
                {
                    if (FileHelpers.ValidImage(model.image) == false)
                    {
                        ModelState.AddModelError("image", "Please choose either a GIF, JPG or PNG image.");
                    }
                }

                if (ModelState.IsValid)
                {
                    string previous_path = model.image_path;
                    path = FileHelpers.UploadImage(model.image);
                    model.image_path = path;
                    model.image = null;
                    model.updated_by = UserHelpers.GetUser().id;

                    SetRequestURL(APIURL.SLIDE_UPDATE, Method.POST);
                    request.AddBody(model);

                    var response = rest.Execute(request);

                    if (response.StatusCode == HttpStatusCode.OK)
                    {
                        // DELETE PREVIOUS IMAGE
                        FileHelpers.DeleteFile(previous_path);

                        SetMessage(Message.SuccessfulUpdate("Slide"), MESSAGE_TYPE.SUCCESS);
                        return RedirectToAction("Index");
                    }
                    else
                    {
                        FileHelpers.DeleteFile(path);
                        ModelState.AddModelError("", response.Content);
                    }
                }
            }
            catch (Exception ex)
            {
                FileHelpers.DeleteFile(path);
                ModelState.AddModelError("", ex.Message);
            }

            SetTitle("Edit - Slider " + model.id);
            return View(model);
        }