Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6797707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:38:31+00:00 2026-05-26T18:38:31+00:00

I have a model that I am using to present an index of a

  • 0

I have a model that I am using to present an index of a model from a database and have given a display name to some of the rows that may need spaces in them, (I.e. “weekstarting” in a db would be given a display name of “Week Starting”).

So I set the display name for my model like this:

 [DisplayName("Week Starting")]
    public DateTime WeekStarting { get; set; }

and then in the table headers for my table I use the following line of code to display the field name using its given display name:

@Html.LabelFor(x => x.First().WeekStarting)

The above all works fine. But I am using the W3C validator and it is giving me the following error for the example I have given:

The for attribute of the label element must refer to a form control.

Forgive me if it is obvious but what am I doing wrong here? I am not using a form I am simply displaying an index of items in a table. I have tried to look for an answer and saw someone suggest that the form controls being referred to need ids (even though I’m not using a form) but this would not be applicable in this instance because if I tried to set an id in the index it would be duplicated with each item in the index:

  foreach (var item in Model.Tbms)
{
<tr><td>@item.value</td><tr>.... would be repeated for each item, and also unsure where I would put the id in any case, the td?
}

Or is there a better way to label the field header, with my preferred display name in the first place? I guess I could just swap @Html.LabelFor… for Hard code field name but do I have to?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T18:38:32+00:00Added an answer on May 26, 2026 at 6:38 pm

    It’s inserting a label element, which should correspond to a valid input on the page. You can (a) either not worry about it, (b) output the text directly without using LabelFor, (c) write a custom helper that extracts the label text from the model without wrapping it in a label, or (d) unwrap it with javascript on the client (which will make it pass validation, but only after running the script).

    Here’s an extension based on the LabelFor code that should help with (c). Untested.

     public static class HelperExtensions
     {
          public static MvcHtmlString TableHeaderFor<TModel,TValue)( this HtmlHelper<TModel> html, Expression<Func<TModel,TValue>> expression )
          {
               var metadata = ModelMetadata.FromLambdaExpression(expression, html.ViewData);
               string headerText = metadata.DisplayName ?? metadata.PropertyName ?? ExpressionHelper.GetExpressionText(expression);
               return new MvcHtmlString( headerText );
          }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some Model code that requires some methods from the System.IO.Compression namespace. However
Using Mongoid 2.4.5 on Rails 3.2.1 I have a Model Book that has_many :pages
I am using javascript unobtrusive validation. I have a view model that I am
I have 2 scenarios that I need some help with re validation in my
I have a domain model that looks like this: case class Account(id: Int, name:
I have a model that represents paintings I present on my site. On the
Let's assume I have two gmdistibution models that i obtained using modeldata1=gmdistribution.fit(data1,1); modeldata2=gmdistribution.fit(data2,1); Now
I have a CheckBox in my application that is using the TriState mode. The
I'm using ajax requests to populate modal popup windows with data that users have
I have a model that looks like this: Performance - Location - Event -

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.