开发者

MvcContrib CheckBoxList

开发者 https://www.devze.com 2022-12-29 13:53 出处:网络
May be some one could explain that behavior: I am using CheckBoxList from the latest version of MvcContrib

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.

Sour开发者_高级运维ce 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?


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号