Razor pages - .cshtml files and tag helpers

We have a section of code like this in a Razor page (.cshtml) file:

<div class="form-group">
	<label asp-for="Input.UserName" class="control-label">User</label>
	<input asp-for="Input.UserName" class="form-control" placeholder="User" autofocus>
</div>

It triggers a warning that issue that a label within a form should be associated with a control. However, in the generated html, the html ‘for’ attribute appears to be generated correctly.

<div class="form-group">
	<label class="control-label" for="Input_UserName">User</label>
	<input class="form-control" placeholder="User" autofocus="" type="text" data-val="true" data-val-required="The UserName field is required." id="Input_UserName" name="Input.UserName" value="">
</div>

It seems like it’s not aware of the asp-for label tag helper. Is this a false positive? Is it expected based on where support for Razor pages is at right now? Or do I most likely just have something misconfigured?

Welcome to the community, @embell-relias,

Would you mind sharing which rule exactly is triggered by this code (rule ID such as S6853 for example, as well as the message of the rule).
This would help us answer your question.

Denis

No problem.

The rule triggered is Web:S6853.

And the specific message it gives is this: " A form label must be associated with a control. Label elements should have a text label and an associated control",

That is what I thought.

Indeed this might be an FP because the generated HTML will be conformant. I will pass that on to the proper team!

Thank you for reporting this, @embell-relias !

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.