Understanding the label helper method, specifically the "for" property of the rendered HTML
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", Mod开发者_运维技巧el.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?
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)
精彩评论