// 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 layoutID, int mediaID)
        {
            var lmToUpdate = await _context.LayoutMedias.FindAsync(layoutID, mediaID);

            if (lmToUpdate == 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 != LayoutMedia.MediaID)
            {
                // delete EM and make new one
                try
                {
                    _context.LayoutMedias.Remove(lmToUpdate);
                    await _context.SaveChangesAsync();

                    var emptyLM = new LayoutMedia();

                    emptyLM.LayoutID = layoutID;

                    if (await TryUpdateModelAsync <LayoutMedia>(
                            emptyLM,
                            "layoutmedia",
                            d => d.MediaID, d => d.MediaLabel))
                    {
                        _context.LayoutMedias.Add(emptyLM);
                        await _context.SaveChangesAsync();

                        return(RedirectToPage("/Layouts/Details", new { id = layoutID }));
                    }

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

            // otherwise just update
            if (await TryUpdateModelAsync <LayoutMedia>(
                    lmToUpdate,
                    "layoutmedia",
                    d => d.MediaID, d => d.MediaLabel))
            {
                await _context.SaveChangesAsync();

                return(RedirectToPage("/Layouts/Details", new { id = layoutID }));
            }

            return(Page());
        }
Exemple #2
0
        // To protect from overposting attacks, see https://aka.ms/RazorPagesCRUD
        public async Task <IActionResult> OnPostAsync(int layoutID)
        {
            var emptyLM = new LayoutMedia();

            emptyLM.LayoutID = layoutID;

            if (await TryUpdateModelAsync <LayoutMedia>(
                    emptyLM,
                    "layoutmedia", // Prefix for form value.
                    d => d.MediaID, d => d.MediaLabel))
            {
                _context.LayoutMedias.Add(emptyLM);
                await _context.SaveChangesAsync();

                return(RedirectToPage("/Layouts/Details", new { id = layoutID }));
            }

            return(Page());
        }