Пример #1
0
        // To protect from overposting attacks, enable the specific properties you want to bind to.
        // For more details, see https://aka.ms/RazorPagesCRUD.
        public async Task <IActionResult> OnPostAsync(int enemyID, int mediaID)
        {
            var emToUpdate = await _context.EnemyMedias.FindAsync(enemyID, mediaID);

            if (emToUpdate == null)
            {
                return(NotFound());
            }

            // if we've changed the MediaID (which is part of the key)
            // then we actually need to delete this EM and make a new one
            if (mediaID != EnemyMedia.MediaID)
            {
                // delete EM and make new one
                try
                {
                    _context.EnemyMedias.Remove(emToUpdate);
                    await _context.SaveChangesAsync();

                    var emptyEM = new EnemyMedia();

                    emptyEM.EnemyID = enemyID;

                    if (await TryUpdateModelAsync <EnemyMedia>(
                            emptyEM,
                            "enemymedia",
                            d => d.MediaID, d => d.MediaLabel))
                    {
                        _context.EnemyMedias.Add(emptyEM);
                        await _context.SaveChangesAsync();

                        return(RedirectToPage("/Enemies/Details", new { id = enemyID }));
                    }

                    return(Page());
                }
                catch (DbUpdateException /* ex */)
                {
                    //Log the error (uncomment ex variable name and write a log.)
                    return(RedirectToAction("./Edit",
                                            new { enemyID, mediaID, saveChangesError = true }));
                }
            }

            // otherwise just update
            if (await TryUpdateModelAsync <EnemyMedia>(
                    emToUpdate,
                    "enemymedia",
                    d => d.MediaID, d => d.MediaLabel))
            {
                await _context.SaveChangesAsync();

                return(RedirectToPage("/Enemies/Details", new { id = enemyID }));
            }

            return(Page());
        }
Пример #2
0
        // To protect from overposting attacks, see https://aka.ms/RazorPagesCRUD
        public async Task <IActionResult> OnPostAsync(int enemyID)
        {
            var emptyEM = new EnemyMedia();

            emptyEM.EnemyID = enemyID;

            if (await TryUpdateModelAsync <EnemyMedia>(
                    emptyEM,
                    "enemymedia", // Prefix for form value.
                    d => d.MediaID, d => d.MediaLabel))
            {
                _context.EnemyMedias.Add(emptyEM);
                await _context.SaveChangesAsync();

                return(RedirectToPage("/Enemies/Details", new { id = enemyID }));
            }

            return(Page());
        }