0

I am trying to use Html.LabelFor like this but it doesn't render properly i.e instead of showing ProviderName value like "AT&T" its just displaying ProviderName text and not the value of ProviderName.

<div id="ServiceProvider" class="main_filter" name="filterDiv">
    <h4>Service Providers</h4>
    @for (var i = 0; i < allProviders.Count(); i++)
    {
        <div class="filter_chkbox_div">
            @Html.CheckBoxFor(m => allProviders[i].Selected, new { value = allProviders[i].ProviderCode, id = allProviders[i].ProviderCode, style="display:none" })
            @Html.LabelFor(m => allProviders[i].ProviderName)
        </div>
    }

Whereas when I use Html.DisplayFor like this:

<label>@Html.DisplayFor(m => allProviders[i].ProviderName)</label>

it displays the value of ProviderName. Whats wrong with my code? I want to use Html.LabelFor. Please help

Huma Ali
  • 1,759
  • 7
  • 40
  • 66
  • `LabelFor()` displays the property name (or the value of `DisplayAttribute.Name` if applied) - that's its purpose. In your case you can use `@Html.LabelFor(m => allProviders[i].ProviderName, allProviders[i].ProviderName)` to display the value of the property but associate the label with the checkbox (but as noted previously, your code will fail because of your incorrect use of `CheckBoxFor()`) –  Apr 21 '16 at 11:40
  • Ok. Actually I want to give a specific Ids to the `ProviderName`. Is it possible to give and id to DisplayFor()? Something like `` – Huma Ali Apr 21 '16 at 11:42
  • What do you mean give `id`? `DisplayFor()` just generates some text. You cannot add attributes because it does not generate an element. –  Apr 21 '16 at 11:43
  • Start by removing `id = allProviders[i].ProviderCode` and the label will be associated with the checkbox. –  Apr 21 '16 at 11:47
  • Can you post this as an answer? Its not clear through your comments – Huma Ali Apr 21 '16 at 11:48

1 Answers1

0

The purpose of LabelFor() is to display the name of the property (or if the property includes a [Display] attribute, then the value of its Name property), and to associate it with a form control (clicking on it will set focus to the control).

However there is are overloads that allows your specify the text to be displayed, so in your case it would be

@Html.LabelFor(m => allProviders[i].ProviderName, allProviders[i].ProviderName)

which will generate

<label for="allProviders_0__ProviderName">AT&T</label>

however this will not act as a label (to set focus) because you have overridden the id attribute of the associated control. Its not clear why your would do that (and if ProviderCode is not unique then you generating invalid html), but you should be removing new { id = allProviders[i].ProviderCode } from the CheckBoxFor() method so that the the default id attribute (id="allProviders_0__ProviderName") is generated.

Side note: You use of new { value = allProviders[i].ProviderCode } means that model binding will fail (you should never set the value attribute when using the HtmlHelper methods). Your property Selected is typeof bool but ProviderCode is not. You are generating a value attribute which cannot be bound to a bool and the result will be that if the checkbox is checked it will submit (say) "ABC", binding will fail and the value will be set to false, and if its unchecked, the value will also be set to false.