-1

I have a HttpGet route called EmailOptions() and a HttpPost route also called EmailOptions(OptionsVm emailOptions). At this point, they are returning a view with the same name (EmailOptions.cshtml) that has a OptionsVm as it's view model. The OptionsVm contains a domain object that is a few boolean properties as well as a string used to display a message.

Once I set some of the OptionsVm's domain object bools to true and post to the EmailOptions(OptionsVm emailOptions), I set a string to have a simple message and I reset the domain object to a brand new domain object (so anything that was set as true should revert back to false).

What I expect to see is the same page as before, with the bool (checkboxes) unchecked with the only change being there is a string with a message to be displayed. However, while it does set the page with the new string value, the bool checkboxes retain their previous checked state from before and. I know that this does go to the correct post route (the string gets set with the new value after all), I just do not understand why the domain model seems to retain it's previous values, especially as I am explicitly setting the domain model to a new domain model.

My view seems to be caching? But that doesn't make sense.. Anyone have an idea why this is happening?

Some code:

// controller 
public class AController : Controller 
{
    ...
    public ActionResult EmailOptions()
    {
        ...
        return View(
          new OptionsVm {
              DomainObj = new DomainObj()
          }
        )
    }

    [HttpPost]
    public ActionResult EmailOptions(OptionsVm vm)
    {
        ... // don't do anything with the actually passed in OptionsVm
        return View(
          new OptionsVm {
              DomainObj = new DomainObj(),
              StringContent = "some message"
          }
        )
    }
}


// viewmodel
public class OptionsVm
{
    public BooleanObjs DomainObj { get; set; }
    public string MessageSometimes { get; set; }
}
mche
  • 616
  • 10
  • 16
  • 1
    Please provide us with some code... A big wall of text does no good. – Helpha Sep 10 '18 at 20:32
  • @Helpha added. controller and vm – mche Sep 10 '18 at 20:40
  • `[HttpPost]` attribute is missing. For example, `[HttpPost]public ActionResult EmailOptions(OptionsVm vm)`. – Win Sep 10 '18 at 20:53
  • @Win you're right - I've added it in the code snippet above, it's in my actual code, just forgot to include it in the above sampling. – mche Sep 10 '18 at 20:54
  • Is it only input values that seem cached? What if you display some text on the page. I asked because many browsers will restore input state in certain situations. This is a nicety they have implemented so that if a user accidentally refreshes the page, or goes forwards then back, all their typing(imagine a long text input) isn't lost. Add some sort of non-input text like a label based on a unique value in the model so you can truly determine if a cached copy is being returned. – AaronLS Sep 10 '18 at 21:03
  • Secondly you can use the network inspector in the browser to see the actual page being returned and check the values of the input fields. If the browser is setting the fields then you'll be able to tell what the true values are being returned over the network. – AaronLS Sep 10 '18 at 21:04
  • 1
    Its a `ModelState` issue. Refer [TextBoxFor displaying initial value, not the value updated from code](https://stackoverflow.com/questions/26654862/textboxfor-displaying-initial-value-not-the-value-updated-from-code/26664111#26664111) to explain the behavior –  Sep 10 '18 at 21:36
  • 2
    The correct solution is to follow the PRG pattern, and redirect back to your GET method, not return the view –  Sep 10 '18 at 21:37
  • @AaronLS - I wonder if that is what's happening. It would be frustrating if that were the case because I'm not sure there is a way for me to verify for certain if that is what is happening unless there's a browser setting that I can set. I do have "disable cache while devTools is open (in chrome)". I did try putting a debugger in the cshtml view and the model is not the new empty DomainObj, it is the model that is the from the vm during the post. – mche Sep 10 '18 at 21:39
  • I believe @StephenMuecke is correct. Will test around and confirm if it is in fact a `ModelState` issue. – mche Sep 10 '18 at 21:42
  • Yup. @StephenMuecke was correct. Thanks! – mche Sep 10 '18 at 21:56

1 Answers1

0

First of all, I'm not sure that you can return a view with a POST action.

So from there I see 2 options:

Maybe you could use AJAX to POST data to your controller and get a JSON response with your text in it.

OR

Maybe you could use RedirectToAction("name", model) in your post controller to redirect to a GET controller.

Helpha
  • 452
  • 6
  • 17