0

I have a view model with a property Value. No matter what I set that property to in an http Post, the text area is rendered empty.

In the ActionResult:

model.Value = "OVERRIDE EVERYTHING AND THIS IS THE VALUE";
return View(model);

In the View (.cshtml):

  @switch (Model.Type.name)
                {

                    case "Content":
                        @Html.TextAreaFor(m => m.Value, htmlAttributes: new { @class = "form-control content-input ckeditor", id = "content" })
                        break;
                }

If I use breakpoints, I can tell that Model.Value in the switch case has the value set. I can also dump the value on the page with @Model.Value, but the line for generating the TextArea never displays the text "OVERRIDE EVERYTHING AND THIS IS THE VALUE" that I would expect.

Thanks in advance,

Edit: I believe this answer on StackOverflow is relevant. https://stackoverflow.com/a/707601/4067801

Community
  • 1
  • 1
user49438
  • 889
  • 7
  • 20
  • 2
    maybe it's a conflicting name.. can you change the property name to something other than Value? – JamieD77 Aug 27 '15 at 18:06
  • Changing the name to Test seemed to have no impact. – user49438 Aug 27 '15 at 19:31
  • It doesn't clear, provide more info, show your view model,... – Sirwan Afifi Aug 27 '15 at 20:06
  • can you share your view and model?, is working for me, are you sure that is going to the case on the switch?? what values can have Model.Type.name?? are you sure that is "Content" and not content? – Daniel Gpe Reyes Aug 27 '15 at 21:14
  • Positive that the switch is working. The value is blank, it isn't that the code isn't run. Plus I can step through it in the debugger. I believe I may have found a similar question elsewhere on StackOverflow. I need to read a bit more. – user49438 Aug 27 '15 at 22:05
  • @user49438, You should not be changing values of the model in the POST method. You should be doing this correctly by following the RPG pattern. But to explain why your not seeing the changes, refer [this answer](http://stackoverflow.com/questions/26654862/textboxfor-displaying-initial-value-not-the-value-updated-from-code/26664111#26664111) - Htmlhelpers use values from `ModelState` where they exist (which they do in a POST method) rather than model properties –  Aug 28 '15 at 00:30

1 Answers1

0

I've had this issue before. The way I got around it was clearing the modelstate value for that particular property. However, the better way to fix this would be to use a redirect back to action.

tcrite
  • 523
  • 2
  • 12