i have a problem with asp.net mvc 2 and the html.textboxfor helper. i use the follow code in a form:
<%= Html.TextBoxFor(model => model.Zip, new { @class = "txt", id = "zip", tabindex = 1 })%>
when the user send the form, i validate the zipcode, when the zip is invalid we set the corrected zip. my model has the corrected zip, the generated html code from asp contains the old zip value.
sample: user write zip: 12345 my validation class, corrected teh zip to: 12346 my model contains the new zip: 123456, on the gui i see only 12345
what is the problem?
You cannot modify the values in your controller action because the helper will always use the POSTed values when generating the textbox. This is by design and if you want to workaround it you will have to write your own helper or generate the textbox manually:
<input
type="text"
name="Zip"
value="<%= Html.Encode(Model.Zip) %>"
class="txt"
id="zip"
tabindex="1"
/>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With