0

I have a table with a couple of boolean values. When certain values change from false to true, i.e. a user checks a checkbox, I have to do some things like send out e-mails.

My current approach is to set a variable with the current value when the user loads the page where he can change the value and then in my Post method I check if my current value is different from the original.

That didn't work though. I don't understand why, but the value would always update between the get and the post method(i.e. while I'm changing the value on the page).

During debugging I mistakenly thought the get method was being called before calling the post for some reason but that was because I was redirecting to the same page(on purpose). However, I seem to not have quite understood how Razor Pages work.

However, I compensated for that by setting a bool to false when opening the page and then only setting the original bool when that's set to false. In my post method I then set it to false again. After doing that suddenly it worked. Kind of. In the post it checks if the value has changed and it actually detects if it has but it doesn't do anything. I've added a toast notification and(because I initially thought the toast was bugged) a Console writeline. I watched the app go over those commands in the debugger and completely ignoring them. I don't understand what's happening anymore. Help!

   private bool Original { get; set; }
   private bool OriginalChanged = false;
   [BindProperty]
   public Tasks Tasks { get; set; }

   public async Task<IActionResult> OnGetAsync(int? id)
    {
        Tasks = await _context.Tasks.FirstOrDefaultAsync(t => t.personalID == id);

        if (Tasks.VertragUnterschriebenZurueck && !OriginalChanged) { Original = true; OriginalChanged = true; }
        else if (!Tasks.VertragUnterschriebenZurueck && !OriginalChanged) { Original = false; OriginalChanged = true; }

        System.Console.WriteLine("--------------------------------------------------------");
        System.Console.WriteLine(Original);
        System.Console.WriteLine("--------------------------------------------------------");

        if (Tasks == null)
        {
            return NotFound(Tasks.personalID);
        }
        return Page();
    }

    public async Task<IActionResult> OnPostAsync()
    {
        if (!ModelState.IsValid)
        {
            return Page();
        }

        System.Console.WriteLine("--------------------------------------------------------");
        System.Console.WriteLine(Original);
        System.Console.WriteLine("--------------------------------------------------------");

        bool changed = Original != Tasks.VertragUnterschriebenZurueck;
        if (changed)
        {
            _toastNotification.Success("Vertrag wurde unterschrieben!");

            System.Console.WriteLine("--------------------------------------------------------");
            System.Console.WriteLine("yay?");
            System.Console.WriteLine("--------------------------------------------------------");
            OriginalChanged = false;
        }

        _context.Attach(Tasks).State = EntityState.Modified;

        try
        {
            await _context.SaveChangesAsync();
        }
        catch (DbUpdateConcurrencyException)
        {
            if (!TasksExits(Tasks.personalID))
            {
                return NotFound();
            }
            else
            {
                throw;
            }
        }
        _toastNotification.Success("Änderungen erfolgreich gespeichert!");
        return RedirectToPage("../Home);
    }
Jat
  • 7
  • 3
  • Controllers get instantiated per request, so `Original` will be false on requests to `OnPostAsync()`. You want [`context.Entry(Tasks).OriginalValues`](https://stackoverflow.com/questions/8133974/how-to-get-original-values-of-an-entity-in-entity-framework). – CodeCaster Oct 14 '22 at 12:52
  • Thank you so much for the link! I managed to get it to work with "var originalEntity = _context.Tasks.AsNoTracking().FirstOrDefault(me => me.personalID == Tasks.personalID); var originalValue = (bool)originalEntity.VertragUnterschriebenZurueck;" ´, as suggested in the thread you've linked. – Jat Oct 14 '22 at 13:55

0 Answers0