Asp.NET MVC Html.TextBox refresh problem

三世轮回 提交于 2019-12-04 03:01:39

问题


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?


回答1:


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



回答2:


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



来源:https://stackoverflow.com/questions/2585472/asp-net-mvc-html-textbox-refresh-problem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!