-1

Doesn't seem like this should work so I have no idea why it does. I LIKE the result, but worried I can't depend on it because I have no idea how it is working.

[HttpGet]  
public ActionResult Modify(System.Guid id)
{
  return View("Modify", LoadFromDatabase(id));
}


[HttpPost]
public ActionResult Modify(CaseModel myModel)
{
  //So odd behavior. If I redirect to the actual GET Modify, 
  //I loose any changes on the form. however if I perform the 
  //same actions but here in the Post... all my unsaved changes 
  //stick..why?

  //This one wipes any edits
  return RedirectToAction("Modify", new { id = myModel.ID});

  //This one actually leaves all my changes, even though 
  //I am re-creating the model from the database just like
  //the other ActionResult
  return View("Modify", LoadFromDatabase(myModel.ID));
}       
da_jokker
  • 954
  • 2
  • 12
  • 16
  • 1
    You can depend on it. `RedirectToAction()` is redirecting to a new page which initializes a new instance of `CaseModel` based on `ID` property (i.e. reads the values from your repository. `return View()` is returning the current instance of `CaseModel` and if you using the `HtmlHelper` methods to generate form controls, it will use the values from `ModelState` which are added by the `DefaultModelBinder` when you submit the form –  Dec 04 '16 at 23:01
  • Refer [this answer](http://stackoverflow.com/questions/26654862/textboxfor-displaying-initial-value-not-the-value-updated-from-code/26664111#26664111) for more explanation of the behavior). `@Html.TextBoxFor(m => m.someProperty)` will display the value you posted, whereas `@Model.someProperty` will display the 'updated' value you set in the post method –  Dec 04 '16 at 23:03
  • Perfect!... I did not know that the HTML helpers overwrite my Model values with the ModelState (aka ViewState?).... That explains it... THANKS! – da_jokker Dec 06 '16 at 16:29

1 Answers1

0

As Stephen Muecke mentions in the two comments above,

You can depend on it. RedirectToAction() is redirecting to a new page which initializes a new instance of CaseModel based on ID property (i.e. reads the values from your repository. return View() is returning the current instance of CaseModel and if you using the HtmlHelper methods to generate form controls, it will use the values from ModelState which are added by the DefaultModelBinder when you submit the form. Refer this answer for more explanation of the behavior). @Html.TextBoxFor(m => m.someProperty) will display the value you posted, whereas @Model.someProperty will display the 'updated' value you set in the post method

works perfectly. The HTML helpers overwrite my Model values with the ModelState (aka ViewState).

Community
  • 1
  • 1
da_jokker
  • 954
  • 2
  • 12
  • 16