コード例 #1
0
        public ActionResult Edit(ExampleParentViewModel model)
        {
            if (ModelState.IsValid)
            {
                var parent = _exampleParentsRepository.Get(model.Id);
                parent.Text     = model.Text;
                parent.LongText = model.LongText;

                if (model.RemovePhoto)
                {
                    _imagesRepository.Remove(parent.PhotoId.Value);

                    parent.PhotoId = null;
                    _exampleParentsRepository.Update(parent);
                    _exampleParentsRepository.SaveChanges();

                    _imagesRepository.SaveChanges();
                }
                else if (model.Upload != null)
                {
                    try
                    {
                        var image = new Photo()
                        {
                            ObjectId       = parent.Id,
                            ObjectTypeName = nameof(ExampleParent),
                            FileName       = $"Image{parent.Id}",
                            DirectoryPath  = "ExampleParents",
                            Title          = "NoTitle"
                        };

                        _imagesRepository.Upload(image, model.Upload);
                        _imagesRepository.SaveChanges();

                        parent.PhotoId = image.Id;
                    }
                    catch (Exception ex)
                    {
                        ModelState.AddModelError(string.Empty, ex.Message);
                    }
                }

                _exampleParentsRepository.Update(parent);
                _exampleParentsRepository.SaveChanges();

                return(RedirectToAction(JMap.Example.ExampleParents.List()));
            }

            return(View(model));
        }
コード例 #2
0
        public ActionResult Create(ExampleParentViewModel model)
        {
            if (ModelState.IsValid)
            {
                var parent = new ExampleParent()
                {
                    Text     = model.Text,
                    LongText = model.LongText
                };

                _exampleParentsRepository.Create(parent);
                _exampleParentsRepository.SaveChanges();

                if (model.Upload != null)
                {
                    try
                    {
                        var photo = new Photo()
                        {
                            ObjectId       = parent.Id,
                            ObjectTypeName = nameof(ExampleParent),
                            FileName       = $"Image{parent.Id}",
                            DirectoryPath  = "ExampleParents",
                            Title          = "NoTitle"
                        };

                        _imagesRepository.Upload(photo, model.Upload);
                        _imagesRepository.SaveChanges();

                        parent.PhotoId = photo.Id;
                        _exampleParentsRepository.Update(parent);
                        _exampleParentsRepository.SaveChanges();
                    }
                    catch (Exception ex)
                    {
                        ModelState.AddModelError(string.Empty, ex.Message);
                    }
                }

                return(RedirectToAction(JMap.Example.ExampleParents.List()));
            }

            return(View(model));
        }
コード例 #3
0
        public ActionResult Create()
        {
            var model = new ExampleParentViewModel();

            return(View(model));
        }