gpt4 book ai didi

MvcContrib 复选框列表

转载 作者:行者123 更新时间:2023-12-02 02:33:36 26 4
gpt4 key购买 nike

可能有人可以解释这种行为:

我正在使用最新版本的 MvcContrib 中的 CheckBoxList当我的页面第一次加载时 - 我只是返回我的 View

return View(Product.GetProduct(productId)); 

一切似乎都很好。所有 html 简单控件填充成功,包括复选框列表:

<%= this.CheckBoxList(model => model.Product.Statuses)
.Options(Model.Statuses, model => model.Id, model => model.Name)
.ItemFormat("{0}<br />")
%>

所以,我在这个表单上有几个按钮,例如按钮“搜索”()。我可以按 productId 搜索并在找到任何内容时显示它。所以我将 productId 传递给我的 Controller ,并且该 Controller 以与第一次相同的方式返回 View :

return View(Product.GetProduct(productId))

顺便说一下,我使用的是相同的逻辑——完全相同:相同的 View 、相同的 Controller 、相同的操作……没有什么新鲜事。但在那种情况下,我收到了这条错误信息:

String was not recognized as a valid Boolean. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.FormatException: String was not recognized as a valid Boolean.

来源错误:

Line 268:                              <labe**strong text**l for="group<%=item.Value%>"><%=item.Text%></label><br />
Line 269: <% } %>--%>
Line 270: <%= this.CheckBoxList(model => model.Product.Statuses).Options(Model.Statuses, model => model.Id, model => model.Name).ItemFormat("{0}<br />")%>
Line 271: </div>
Line 272: </div>

我发现,当第一次和之后加载 View 时,如果所有复选框都未选中,我正在单击搜索 - 一切顺利,但当任何复选框被选中时,我正在单击搜索 - 我收到此错误。

我需要帮助。有什么好主意吗?

最佳答案

我认为 CheckBoxList 中存在错误,因此当它验证列表时,它要求复选框的值是可转换的 bool 字符串(“false”、“true”)。

因此在您的示例中,如果您将 model => model.id 更改为 model => "true",您会发现不会收到错误:

 <%= this.CheckBoxList(model => model.Product.Statuses).Options(Model.Statuses, model => "true", model => model.Name).ItemFormat("{0}<br />")%>

但这不是你想要的。我的解决方法是在 ModelState 无效时从 ModelState 中删除 CheckBoxList 的元素(在您的情况下为 model.Product.Statuses)。

需要在ModelState中找到model.Product.Statuses对应的key并移除。我正在为我的案例使用以下代码段。您需要更改模型和属性。

if (!ModelState.IsValid)
{
ModelState.Remove(PropertyHelper<EmailModel>.GetProperty(x => x.Attachments).Name);
...

我正在使用 PropertyHelper 表单 How to get the PropertyInfo of a specific property?

我还在 mvccontrib 问题跟踪器中找到了这个相关链接: http://mvccontrib.codeplex.com/workitem/7071

关于MvcContrib 复选框列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2800584/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com