1

I am implementing it so I can load a json file as an options file and persist the changed data if the need be. I followed this SO question and I have it properly working the file saves and I successfully get the trigger for reload on change. However when I POST the action and update the information and at the end execute RedirectToAction("Index") it doesn't seem to update the Transient. After it returns the Redirect the page is in the same state as before the POST, however when I manually press F5 to refresh the page the proper data is now showing.

I have tried things like RedirectToAction("Index", "Admin") and tried looking into having the transient update every action or writing my own but can't find anything to help me even start doing that.

It would be helpful to understand what IWritableOptions is when looking at the link above. The only thing I changed is instead of giving it a section it loads the whole file.

        private readonly IWritableOptions<Settings> settings;

        public AdminController(IWritableOptions<Settings> settings)
        {
            this.settings = settings;
        }

        public IActionResult Index()
        {
            // Loading for the first time all is good and settings values are valid
            // After being RedirectToAction from the HttpPost the data hasn't updated to the new values.
            // If I manually refresh the page all data is correctly set to the new values. (Same if I leave the page and come back)

            return View(settings.Value);
        }

        [HttpPost]
        public IActionResult General(SettingsViewModel model)
        {
            settings.Update(opts =>
            {
                opts.Value1 = model.Value1 ;
                opts.Value2 = model.Value1 ;
            });

            return RedirectToAction(nameof(Index));
        }

The expected results is after the RedirectToAction the settings values are now set to their new values. I think this has to do with using AddTransient where it only refreshes on a full controller reload but am not 100% positive from my reading. I am up for any possibility to get this working properly...

Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
Lucas
  • 376
  • 3
  • 18
  • Well, that won't work, as the Controller isn't recreated for that Redirect, and you can verify this by just putting a breakpoint on the Controller. Cannot you just do a refresh from JavaScript? – Camilo Terevinto Apr 18 '19 at 23:33
  • I can but I am wanting to try to make it work with out a hack to the page itself so every time I use it or want to update I don't have to drag javascript to each page and instead it handles it for me automatically. Can you make like a custom injection factory so instead of `AddTransient` I can use say `AddSomethingElse` which knows it needs to get a new one every action? I am just wanting something that doesn't need code dragging behind it on the page to make it work if that makes sense. – Lucas Apr 18 '19 at 23:44
  • So setting the values to the object before redirecting does work and is a fine enough work around for me currently but would still potentially prefer a different method. I may try do build it into the update method through reflection. – Lucas Apr 19 '19 at 00:27
  • Try injecting again in your view - or just inject the value in view - both might help – Rahul Apr 19 '19 at 04:58

0 Answers0