MvcContrib 复选框列表

发布于 2024-09-01 02:21:00 字数 1474 浏览 4 评论 0原文

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

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

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 传递给我的控制器,该控制器以与第一次相同的方式返回视图:

return View(Product.GetProduct(productId))

顺便说一下,我使用相同的逻辑 - 都一样:相同的视图、相同的控制器、相同的操作……没什么新意。但在这种情况下,我收到此错误消息:

字符串未被识别为有效的布尔值。 描述:执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其在代码中的来源的更多信息。

异常详细信息:System.FormatException:字符串未被识别为有效的布尔值。

源错误:

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>   

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

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

May be some one could explain that behavior:

I am using CheckBoxList from the latest version of MvcContrib
When my page is loading first time - I am simply return my view

return View(Product.GetProduct(productId)); 

and everything seems to be fine.
All html simple controls populated successful, including checkboxlist:

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

So, I have a couple of buttons on this form, e.g button “Search” (). I can search by productId and display it if anything was found.
So I am passing productId to my controller and this controller returning view the same way as first time:

return View(Product.GetProduct(productId))

by the way I am using the same logic - all the same: the same view, the same controller, the same action… nothing new. But in that case I've got this error message:

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.

Source Error:

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>   

I find out, that when view is loaded first time and after, if all checkboxes unchecked I am clicking search - all going well, but when any of checkboxes checked, I am clicking search – I am getting this error.

I need help. Any bright ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

余生再见 2024-09-08 02:21:00

我相信 CheckBoxList 中存在一个错误,当它验证列表时,它要求复选框的值是布尔可转换字符串(“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 表单 如何获取 PropertyInfo特定属性?

另外,我在 mvccontrib 问题跟踪器中找到了此相关链接:
http://mvccontrib.codeplex.com/workitem/7071

I believe there is a bug in CheckBoxList, such that when it validates the list it requires the values for check boxes to be boolean convertible strings ("false", "true").

So in you example if you change model => model.id to model => "true" you see that you will not get the error:

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

But this is not what you want. My workaround is to remove the element for the CheckBoxList (in your case model.Product.Statuses) from ModelState when ModelState is not valid.

You need to find the corresponding key for model.Product.Statuses in ModelState and remove it. I am using the following snippet for my case. You you need to change the model and property.

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

I am using PropertyHelper form How to get the PropertyInfo of a specific property?

Also I fount this relevant link in mvccontrib issue tracker:
http://mvccontrib.codeplex.com/workitem/7071

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文