asp.net-mvc - 为什么清除模型后文本框不清除

标签 asp.net-mvc

我有一个通过 ajax 提交给 Controller 的表单。 Controller 从模型中收集数据,然后创建一个新的 View 模型并将其传递给局部 View ,然后在客户端上更新。

问题是文本框没有按预期清除。消息按预期到达 View ,因此没有清除 TextBoxes 是没有意义的。

然而,这似乎是一个浏览器问题,因为这是生成的 HTML,请注意,值中没有任何内容:

<input id="Address_Address1" name="Address.Address1" type="text" value="" />

然而,用户会看到之前输入的值。

这是 Controller :
//Process Order Here


//This should clear out the ViewModel.
order = new OrderViewModel();
order.Message = "Report Added to your cart";

return PartialView("_NewOrderPartial", order);

这是部分 View :
@model NTC.PropertySearch.Models.OrderViewModel

@using (Ajax.BeginForm("NewOrder", "Order", new AjaxOptions { InsertionMode =     InsertionMode.Replace, UpdateTargetId = "neworder" }))
{
<div id="neworder">
    <table>
        <tr>
            <th style="width: 300px;">
                <h3>Enter property address below</h3>
            </th>
            <th style="width: 30px;"></th>
            <th style="width: 300px;">
                <h3>Choose your report below</h3>
            </th>
        </tr>
        <tr>
            <td>
                <div class="form">
                    <table>
                        <tr>
                            <td>
                                @Html.LabelFor(m => m.Address.Address1)
                            </td>

                            <td>
                                @Html.TextBoxFor(m => m.Address.Address1)
                            </td>

                            <td>
                                @Html.ValidationMessageFor(m => m.Address.Address1)
                            </td>
                        </tr>

                        <tr>
                            <td>
                                @Html.LabelFor(m => m.Address.Address2)
                            </td>
                            <td>
                                @Html.TextBoxFor(m => m.Address.Address2)
                            </td>

                            <td>
                                @Html.ValidationMessageFor(m => m.Address.Address2)
                            </td>
                        </tr>

                        <tr>
                            <td>
                                @Html.LabelFor(m => m.Address.City)
                            </td>
                            <td>
                                @Html.TextBoxFor(m => m.Address.City)
                            </td>

                            <td>
                                @Html.ValidationMessageFor(m => m.Address.City)
                            </td>
                        </tr>

                        <tr>
                            <td>
                                @Html.LabelFor(m => m.Address.State)
                            </td>
                            <td>
                                @Html.TextBoxFor(m => m.Address.State)
                            </td>

                            <td>
                                @Html.ValidationMessageFor(m => m.Address.State)
                            </td>
                        </tr>


                        <tr>
                            <td>
                                @Html.LabelFor(m => m.Address.ZipCode)
                            </td>

                            <td>
                                @Html.TextBoxFor(m => m.Address.ZipCode)
                            </td>
                            <td>
                                @Html.ValidationMessageFor(m => m.Address.ZipCode)
                            </td>
                        </tr>

                    </table>

                    <input type="submit" value="Add Report" />
                    @Html.DisplayFor(m=> m.Message)

                </div>
            </td>
                        </tr>
    </table>
</div>        

最佳答案

这样做的原因是因为像 TextBox 这样的 Html 助手在绑定(bind)它们的值时首先查看 ModelState,然后才在 View 模型中查看。当人们尝试在 HttpPost Controller 操作中修改/清除 View 模型的某些值时,通常会发生这种情况。相应的 View 将使用初始 POSTed 值,而不是 View 模型中的值。

您还应该在 Controller 操作中清除 ModelState:

ModelState.Clear();
order = new OrderViewModel();

或者,如果您不想清除整个 ModelState(尽管在您的情况下似乎是这种情况,因为您正在重新初始化整个 View 模型),您可以只清除您打算修改的一些属性:
ModelState.Remove("SomeProperty");
viewModel.SomeProperty = "some new value";

关于asp.net-mvc - 为什么清除模型后文本框不清除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20639288/

相关文章:

c# - Fluent Api - M :M relationship with two 1:M properties. 怎么样?

javascript - MVC 5 : jQuery nested tabs are not displaying properly

javascript - 动态地将模型行添加到表中

c# - 在 ASP.NET MVC 项目中使用 WebControl

asp.net-mvc - 如何为.net core mvc应用程序集中防止脚本注入(inject)

c# - 在 Kestrel 上为 ASP.NET Core 上的两个不同端点发布两个不同的端点

asp.net-mvc - 更新身份 1 到 2 后,没有 IUserTokenProvider 已注册错误

c# - 在哪里可以找到 ASP.NET MVC 插件模式的介绍?

c# - 如何通过自定义Attribute获取和修改属性值?

asp.net - 您将如何显示在从模板构建的网站上选择了哪个选项卡?