Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asp.NET MVC Html.TextBox refresh problem

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?

like image 785
martin Avatar asked Apr 06 '10 14:04

martin


1 Answers

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" 
/>
like image 68
Darin Dimitrov Avatar answered Oct 09 '22 18:10

Darin Dimitrov