HTML.CheckBox 在 POST 后保持状态 - 刷新 ModelState?
我有一个由许多项目组成的表单(想想亚马逊订单上的订单项目)。每行都有一个与之关联的复选框,因此用户可以选择多个项目并单击“删除”。
表格的构建有点像这样;
<% for (int i = 0; i < Model.OrderItems.Count; i++) { %>
<tr>
<td><%= Html.Hidden(String.Format("OrderItems[{0}].Id", i), Model.OrderItems[i].Id)%>
<%= Html.CheckBox(String.Format("OrderItems[{0}].Checked", i), Model.OrderItems[i].Checked)%></td>
<td><%= Html.TextBox(String.Format("OrderItems[{0}].Name", i), Model.OrderItems[i].Name)%></td>
<td><%= Html.TextBox(String.Format("OrderItems[{0}].Cost", i), Model.OrderItems[i].Cost)%></td>
<td><%= Html.TextBox(String.Format("OrderItems[{0}].Quantity", i), Model.OrderItems[i].Quantity)%></td>
</tr>
<% } %>
模型绑定器很好地发挥了它的魔力,并且列表已正确填充。但是,在我处理操作中的请求(例如删除适当的项目)并返回包含较少项目的新视图后,表单的状态是“半”持久的。即使在编辑模型中所有布尔值都设置为 false,某些复选框仍保持选中状态。
如果我返回一个 RedirectToActionResult
,我就不会遇到这个问题,但使用它作为解决方案似乎有点棘手。
我认为我需要刷新/刷新 ModelState 或类似的东西,但我不确定要搜索的术语以了解如何操作。
I have a form that's made up of many items (think order items on an amazon order). Each row has a checkbox associated with them so the user can select many items and click 'remove'.
The form is built up a bit like this;
<% for (int i = 0; i < Model.OrderItems.Count; i++) { %>
<tr>
<td><%= Html.Hidden(String.Format("OrderItems[{0}].Id", i), Model.OrderItems[i].Id)%>
<%= Html.CheckBox(String.Format("OrderItems[{0}].Checked", i), Model.OrderItems[i].Checked)%></td>
<td><%= Html.TextBox(String.Format("OrderItems[{0}].Name", i), Model.OrderItems[i].Name)%></td>
<td><%= Html.TextBox(String.Format("OrderItems[{0}].Cost", i), Model.OrderItems[i].Cost)%></td>
<td><%= Html.TextBox(String.Format("OrderItems[{0}].Quantity", i), Model.OrderItems[i].Quantity)%></td>
</tr>
<% } %>
The model binder does its magic just fine and the list is correctly populated. However, after I process the request in the action (e.g. remove the appropriate items) and return a new view containing fewer items, the state of the form is 'semi' persisted. Some check boxes remain checked, even though in the edit model all the bools are set to false.
I don't have this problem if I return a RedirectToActionResult
, but using that as a solution seems a bit of a hacky work around.
I think I need to flush/refresh the ModelState, or something similiar, but I'm unsure of the terms to search for to find out how.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果你检查渲染的 HTML,我想你会发现一个与你的复选框同名的隐藏输入;这就是
HTML.Checkbox
的工作方式。它确实会导致提交一组值。这可能是您的问题的原因。
善良,
丹
If you check the rendered HTML I think you will find a hidden input of same name as your checkbox; this is the way the
HTML.Checkbox
works. It does lead to an array of values being submitted.This could be the cause of your problem.
Kindness,
Dan
我想查看您的控制器代码,但认为您的 viewModel 已按惯例填充。
在您的 Index 操作中,我猜您创建了一个新的 viewModel。尝试以不同的方式命名它,例如调用实例indexViewModel,它不会自动收集以前的viewModel数据。
I would like to see your controller code but think you're viewModel is being filled by fact of convention.
In your Index action I'm guessing you create a new viewModel. Try name this differently, call the instance indexViewModel say and it will not automagically gather the previous viewModel data.
您的预感可能是正确的,您可能需要使用
重定向来调用,通常不被视为黑客,它实际上是一个相当好的实践,称为 Post Redirect Get (PRG)。
Your hunch is probably correct, you likely need to call
btw using a Redirect normally isn't considered a hack, it's actually fairly a good practice called Post Redirect Get (PRG).