3

I have a checkbox on my page, created as such:

@Html.CheckBoxFor(x => x.MyBoolValue)   

Beneath that I have a WebGrid with sorting and paging, but on doing this I get a System.InvalidOperationException:

The parameter conversion from type 'System.String' to type 'System.Boolean' failed. See the inner exception for more information.

The inner exception is:

{"true,false is not a valid value for Boolean."}

I know this is due to the binding of the CheckBox and the underlying Hidden value. On a page GET I deal with this using Contains("true") on the query string value, but how can I do this when the WebGrid is refreshed?

pfeds
  • 2,183
  • 4
  • 32
  • 48
  • Take a look at this post: http://stackoverflow.com/questions/11234623/asp-net-mvc-webgrid-is-not-properly-passing-current-parameters-to-the-pagination – CameronP Dec 05 '13 at 17:46

4 Answers4

3

MVC creates two inputs on @Html.CheckBoxFor(x => x.MyBoolValue). One Checkbox and one hidden field with the value false.

He (MVC ;)) does this ensure that, even if you dont check the checkbox, the variable exists. So if you check the box the variable value is true,false and false if not.

do this:

<input type="CheckBox" name="MyBoolValue" value="@Model.MyBoolValue"/>

or create a custom model binder that handles that.

ASP.NET MVC Custom Model Binding

dknaack
  • 60,192
  • 27
  • 155
  • 202
  • Excellent, I knew it would be something simple. I searched Google but had so many hits about checkbox columns within the WebGrid - not that useful for my issue. Thanks. – pfeds Nov 17 '11 at 09:22
  • B*gg*r, I've just set the ajaxUpdateContainerId property on the WebGrid and this causes the same issue, even with Html.CheckBox()... any ideas? – pfeds Nov 17 '11 at 09:28
  • Yes, use a normal html input for that, ive changed my answer. hope this helps – dknaack Nov 17 '11 at 09:30
  • Thanks for your help. I've been working on this issue recently and made some other discoveries. If it helps I've documented it here - http://websitesorcery.com/post/2012/03/19/CheckBox-Issue-with-MVC-3-WebGrid-Paging-Sorting.aspx – pfeds Mar 20 '12 at 01:31
  • This doesn't work for me, the checkbox is not properly bound to the model, and checking the checkbox will not return true in the property on postback. – Tobberoth Jan 17 '14 at 11:41
1

If you want to maintain all standard validators or binders functionality and hidden field bother you someway. You can use this:

<input type="CheckBox"@(Model.BoolProperty ? " checked=\"checked\"" : string.Empty) name="BoolProperty" value="true"/>
Orion
  • 76
  • 3
0

I have found that removing the value from the ModelState resolves this, and doesn't seem to cause any side effects - perhaps someone could point out if I've missed anything?

In my controller I just add

ModelState.Remove("MyBoolValue");

Which means that

@Html.CheckBoxFor(x => x.MyBoolValue)   

will fallback to using the Model value unless I'm mistaken?

MarkD
  • 1,025
  • 8
  • 12
0

The problem is the name of your action parameter:

public ActionResult Product(ProductModel model); 

For example the following will fix your issue:

public ActionResult Product(ProductModel request); 
slfan
  • 8,950
  • 115
  • 65
  • 78
Sonsuz
  • 46
  • 3