コード例 #1
0
        public IActionResult CreateOurWork(OurWorkViewModel model)
        {
            if (ModelState.IsValid)
            {
                if (model.UploadBefore != null)
                {
                    string upload   = Path.Combine(_hosting.WebRootPath, @"image");
                    string fullpath = Path.Combine(upload, model.UploadBefore.FileName);
                    model.UploadBefore.CopyTo(new FileStream(fullpath, FileMode.Create));
                    model.Before = model.UploadBefore.FileName;
                }
                if (model.UploadAfter != null)
                {
                    string upload   = Path.Combine(_hosting.WebRootPath, @"image");
                    string fullpath = Path.Combine(upload, model.UploadAfter.FileName);
                    model.UploadAfter.CopyTo(new FileStream(fullpath, FileMode.Create));
                    model.After = model.UploadAfter.FileName;
                }

                OurWork work = new OurWork
                {
                    Before = model.Before,
                    After  = model.After
                };
                _context.OurWork.Add(work);
                _context.SaveChanges();
                return(RedirectToAction(nameof(OurWorkIndex)));
            }
            return(View(model));
        }
コード例 #2
0
        public IActionResult UpdateOurWork(int?id, OurWorkViewModel viewmodel)
        {
            if (id == null)
            {
                return(NotFound());
            }
            if (ModelState.IsValid)
            {
                try
                {
                    if (viewmodel.UploadBefore != null)
                    {
                        string upload   = Path.Combine(_hosting.WebRootPath, @"image");
                        string fullpath = Path.Combine(upload, viewmodel.UploadBefore.FileName);

                        viewmodel.UploadBefore.CopyTo(new FileStream(fullpath, FileMode.Create));

                        viewmodel.Before = viewmodel.UploadBefore.FileName;
                    }
                    if (viewmodel.UploadAfter != null)
                    {
                        string upload   = Path.Combine(_hosting.WebRootPath, @"image");
                        string fullpath = Path.Combine(upload, viewmodel.UploadAfter.FileName);
                        viewmodel.UploadAfter.CopyTo(new FileStream(fullpath, FileMode.Create));
                        viewmodel.After = viewmodel.UploadAfter.FileName;
                    }

                    var ourwork = new OurWork
                    {
                        Id     = viewmodel.Id,
                        Before = viewmodel.Before,
                        After  = viewmodel.After
                    };
                    _context.Entry(ourwork).State = EntityState.Modified;
                    _context.SaveChanges();
                }
                catch (DbUpdateConcurrencyException)
                {
                    throw;
                }
                return(RedirectToAction(nameof(OurWorkIndex)));
            }
            return(View(viewmodel));
        }
コード例 #3
0
        public IActionResult EditOurWork(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }
            var work = _context.OurWork.Single(s => s.Id == id);

            if (work == null)
            {
                return(NotFound());
            }
            OurWorkViewModel workviewmodel = new OurWorkViewModel
            {
                Before = work.Before,
                After  = work.After
            };

            return(View(workviewmodel));
        }