Search code examples
asp.net-mvcasp.net-mvc-4html.textboxfor

Why does Html.TextBoxFor and Html.LabelFor shows different value?


in my following View why do i get text"name" in label and value of name in textbox?

    @model IEnumerable<string>

    @foreach(var names in Model)
    {
        <div class="label">@Html.LabelFor(m=>names)</div>
        <div class="input-block-level">@Html.TextBoxFor(m=>names)</div>
        <br/>
    }

If i run this I get following view in browser

enter image description here


Solution

  • Html.LabelFor reads the metadata from a model property and displays the name of the property. You can configure the displayed name with the Display or DisplayName attributes. Your model has no properties, so LabelFor is completely useless in your example.

    What do you want to achieve? When you want to render a label with the value of the string just create a label:

    <label>@names</label>