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

InputFor() generates incorrect name value for nested entity #44

Closed frankvaneykelen closed 8 years ago

frankvaneykelen commented 8 years ago

This is how the built-in HTML helpers deal with nested entities:

Razor:

@Html.LabelFor(model => model.Album.Price)
@Html.EditorFor(model => model.Album.Price)

HTML:

<label for="Album_Price">Price</label>
<input class="text-box single-line" id="Album_Price" name="Album.Price" type="text" value="" />

Please note that the dot has been replaced by an underscore for the 'id' and 'for' attributes only.

FluentBootstrap handles it differently (incorrectly):

Razor:

 @Html.Bootstrap().InputFor(model => model.Album.Price)

HTML:

 <div class="form-group">
      <label for="Album_Price" class="control-label col-xs-4">Price</label>
      <div class="col-xs-3">
         <input type="text" name="Album_Price" id="Album_Price" class="form-control">
  </div>

Please note that the dot has been replaced by an underscore for the 'name' attribute too.

I would expect the name of the markup generated by the FluentBootstrap helper to be the same. This also breaks my model binding.

I have changed GetControlName to fix this is my project. It now does this:

return helper.GetConfig().HtmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(expressionText);

I also added these lines to Tag.OnStart():

if (string.Equals(attribute.Key, "id", StringComparison.Ordinal) || string.Equals(attribute.Key, "for", StringComparison.Ordinal))
    {
        encoded = TagBuilder.CreateSanitizedId(encoded);
    }
daveaglick commented 8 years ago

Great catch, thanks. And especially thanks for including a fix :) I'll get this patched up and a new version up on NuGet ASAP (most likely later today).

frankvaneykelen commented 8 years ago

Great! Please make sure that to add CreateSanitizedId()'s everywhere it's needed - I only fixed it for my use case, but there may be others!

daveaglick commented 8 years ago

Can you confirm which version of FluentBootstrap you're using? It looks like this may have been fixed in 3.3.5.0. It looks like I'm currently getting the correct output from the tests:

2015-09-25_13h13_41

josephwoodward commented 8 years ago

I've not been able to replicate it either.

frankvaneykelen commented 8 years ago

@daveaglick version: 3.3.4.4. I'll go and try the latest version.

frankvaneykelen commented 8 years ago

I can confirm that this is fixed in 3.3.5.1. Thanks for your help.

daveaglick commented 8 years ago

Excellent - glad it's working for you now. Just let me know if you run into any other problems.