0

Do not know why MVC5 has stopped binding textbox, the value is only being shown if it was part of the request, not if I set it manually.

This prints the correct property value:

  @Html.Raw(Model.MyStringField)

This prints a input box that does not have a value, i.e. value=""

           @Html.TextBoxFor(m => m.MyStringField, new
                                {
                                    @class = "myCssClass",
                                    placeholder = "Test",
                                    autocomplete = "off"
                                })

What could be going wrong?

MVC Hates me, these code items are on the same line but return different values:

Model.Location = "TEST";
@Html.Raw(Model.Location) // Prints TEST
@Html.TextBox(@"Location", Model.Location) // Prints value from postback
simbolo
  • 7,279
  • 6
  • 56
  • 96

2 Answers2

0

You don't need another @ symbol before "class" and "Location". I think you're trying to start razor syntax, when it's already started and confusing things.

try these edits:

    @Html.TextBoxFor(m => m.MyStringField, new
                            {
                                class = "myCssClass",
                                placeholder = "Test",
                                autocomplete = "off"
                            })
    @Html.TextBox("Location", Model.Location) // Prints value from postback
Chad McGrath
  • 1,561
  • 1
  • 11
  • 17
  • 1
    Thanks; but the @ is actually necessary since 'class' is a reserved keyword and the @ escapes it (won't compile without the @), @"Location" makes a literal string, so stops Visual Studio highlighting it as a 'localisable' string, since in this case it's a variable name and wouldn't change between language since it'll mess up form variable. – simbolo Mar 05 '14 at 06:08
0

Turns our after reading another answer about values not being read after postback is that hte HTML Helpers, whilst told to use model values, actually use the ModelState value. So calling ModelState.Clear(); is needed to reset it and force it to use the model.

Community
  • 1
  • 1
simbolo
  • 7,279
  • 6
  • 56
  • 96