【问题标题】:Asp.NET MVC Html.TextBox refresh problemAsp.NET MVC Html.TextBox刷新问题
【发布时间】:2011-02-04 20:11:55
【问题描述】:

我对 asp.net mvc 2 和 html.textboxfor 助手有疑问。 我在表单中使用以下代码:

<%= Html.TextBoxFor(model => model.Zip, new { @class = "txt", id = "zip", tabindex = 1 })%>

当用户发送表单时,我验证邮政编码,当邮政编码无效时,我们设置更正的邮政编码。 我的模型有更正的 zip,从 asp 生成的 html 代码包含旧的 zip 值。

示例: 用户写 zip:12345 我的验证课,将 zip 更正为:12346 我的模型包含新的 zip:123456,在 gui 上我只看到 12345

有什么问题?

【问题讨论】:

    标签: asp.net asp.net-mvc caching refresh html.textbox


    【解决方案1】:

    您无法修改控制器操作中的值,因为帮助程序在生成文本框时将始终使用已发布的值。这是设计使然,如果您想解决它,您将不得不编写自己的帮助程序或手动生成文本框:

    <input 
        type="text" 
        name="Zip" 
        value="<%= Html.Encode(Model.Zip) %>" 
        class="txt" 
        id="zip" 
        tabindex="1" 
    />
    

    【讨论】:

      【解决方案2】:

      使用 ModelState.Clear() 清除模型状态,更新您的对象,然后返回它。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多