Пример #1
0
        public async Task <IActionResult> Create(FixViewModel fix)
        {
            if (ModelState.IsValid)
            {
                Fix fvm = new Fix
                {
                    ordering  = fix.ordering,
                    heading   = fix.heading,
                    maintext  = fix.maintext,
                    nextvalue = fix.nextvalue,
                    backvalue = fix.backvalue
                };

                IFormFile img = fix.image;
                if (img != null)
                {
                    var filePath = Path.Combine(_rootpath, "images", img.FileName);
                    using (var stream = new FileStream(filePath, FileMode.Create))
                    {
                        await img.CopyToAsync(stream);
                    }
                    //The file has been saved to disk - now save the file name to the DB
                    fvm.image = img.FileName;
                }

                _context.Add(fvm);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(fix));
        }
Пример #2
0
        public async Task <IActionResult> Edit(int id, FixViewModel fvm)
        {
            if (id != fvm.ID)
            {
                return(NotFound());
            }
            //Initialise a new companysegment
            Fix fix = await _context.Fix.FindAsync(id);

            if (ModelState.IsValid)
            {
                try
                {
                    fix.ID        = id;
                    fix.ordering  = fvm.ordering;
                    fix.heading   = fvm.heading;
                    fix.maintext  = fvm.maintext;
                    fix.nextvalue = fvm.nextvalue;
                    fix.backvalue = fvm.backvalue;

                    IFormFile img = fvm.image;
                    if (img != null)
                    {
                        var filePath = Path.Combine(_rootpath, "images", img.FileName);
                        using (var stream = new FileStream(filePath, FileMode.Create))
                        {
                            await img.CopyToAsync(stream);
                        }
                        //The file has been saved to disk - now save the file name to the DB
                        fix.image = img.FileName;
                    }

                    _context.Update(fix);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!FixExists(fix.ID))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(fix));
        }
Пример #3
0
        // GET: Fixes/Edit/5
        public async Task <IActionResult> Edit(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            var fix = await _context.Fix.FindAsync(id);

            if (fix == null)
            {
                return(NotFound());
            }
            FixViewModel fvm = new FixViewModel
            {
                ordering  = fix.ordering,
                heading   = fix.heading,
                maintext  = fix.maintext,
                nextvalue = fix.nextvalue,
                backvalue = fix.backvalue
            };

            return(View(fvm));
        }
Пример #4
0
        // GET: Fixes/Create
        public IActionResult Create()
        {
            FixViewModel fvm = new FixViewModel();

            return(View(fvm));
        }