tags:

views:

35

answers:

2

Hello

I have a page where I list checkboxes named TimeRange1, TimeRange2.... TimeRange7, and I generate them in a for loop like this:

<% for (int currentDay = 1; currentDay <= 7; currentDay++)
......<%= Html.CheckBox("TimeRange" + currentDay.ToString(), false)%>

Works fine til I post the form, then I get a "The string was not identified as a valid Boolean string."

Apperantly the problem is that I concatenate the name of the checkbox.

Is there some neat way of fixing this? I need to have them named 1-7. It's like a big schedule where you select which times should be avalible.

/M

A: 

If you can't solve your problem by using Html.Checkbox, try the alternative approach suggested in the accepted answer of this question: http://stackoverflow.com/questions/220020/how-to-handle-checkboxes-in-asp-net-mvc-forms

Konamiman
+2  A: 

Try without helpers:

<% for (int currentDay = 1; currentDay <= 7; currentDay++) { %>
    <input type="checkbox" name="TimeRange<%= currentDay.ToString() %>" />
<% } %>

Html Helpers get their values from :

  1. ViewData.ModelState["controlName"].Value.RawValue.
  2. value parameter passed to HTML helper method.

They also generate a hidden input field in addition to the checkbox. So depending on the context sometimes you may end up with markup like this:

<input type="checkbox" name="TimeRange1" value="SomeValue, false" />

and when you post the form the data binder will be unable to convert the value to a boolean type.

Darin Dimitrov