views:

15

answers:

1

I have a strongly typed partial view CheckBox.ascx that renders a checkbox. This is used by another partial view that renders a list of checkboxes using CheckBox.ascx.

I am having problems figuring out how to use the Html.Label helper method to get labels working correctly, i.e. setting the label for property to be the id of the related checkbox.

My view markup for the checkbox looks like this: (I'm using non-sequential list binding as described under Non-Sequential Indices)

<%= Html.CheckBox(string.Format("checkBoxes[{0}].Checked", Model.Id), Model.Checked)%>

The complexity here is that my checkbox ID will be mangled - [, ], . characters replaced with _.

Can the Html.Label helper method help me here?

A: 

My solution was to manually code the label element...

<label for="<%= string.Format("checkBoxes_{0}__Checked", Model.Id)%>"><%= Html.Encode(Model.Label)%></label>

(noting that the ., [ and ] characters in the checkbox name get rendered as underscores)

Richard Ev