martin
martin

Reputation: 93

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?

Upvotes: 9

Views: 5111

Answers (2)

Gokulnath
Gokulnath

Reputation: 1256

Clear the modelstate using ModelState.Clear(), update your object and then return it.

Upvotes: 2

Darin Dimitrov
Darin Dimitrov

Reputation: 1039130

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" 
/>

Upvotes: 8

Related Questions