daveaglick / FluentBootstrap

Provides extensions, helper classes, model binding, and other goodies to help you use the Bootstrap CSS framework from .NET code.
http://www.fluentbootstrap.com
MIT License
200 stars 76 forks source link

Populate the label 'for' with the control id #41

Closed liamdavies closed 9 years ago

liamdavies commented 9 years ago

The label 'for' attribute should reflect the id of the labelled control, not the name.

The population of the 'for' attribute for the label has been tied to the name field of the attached control. This has worked fine when using a simple property in the control expression, since the id and name are always equivalent, such as:

Html.Bootstrap().InputFor(x => x.PropA)
generates
    <label for="PropA" class="control-label">Property A</label>
    <input type="text" name="PropA" id="PropA" class="form-control">

However if a complex expression is used, then the input 'id' must be sanitized, and the name must remain unsanitized so that the complex model can be model bound when posted. With the existing code the name is also sanitized, which breaks the default MVC model binding for complex properties.

Html.Bootstrap().InputFor(x => x.Child.ChildPropA)
should generate
    <label for="Child_ChildPropA" class="control-label">Child Property A</label>
    <input type="text" name="Child.ChildPropA" id="Child_ChildPropA" class="form-control">

This change implements this correct behaviour for complex properties.

daveaglick commented 9 years ago

This looks excellent, thanks a lot. I'll pull it in tomorrow and prepare a new release for NuGet.

daveaglick commented 9 years ago

Thanks again - releasing to NuGet in an hour or two (once I get everything updated to Bootstrap 3.3.5)