Tag Helpers - Self closing HTML tags is a bad habit?
Asked Answered
A

1

7

I am incorporating TagHelpers in my MVC vNext project, and I realized they don't work when I self close the HTML tags.

@addTagHelper "*, Microsoft.AspNet.Mvc.TagHelpers"

<label asp-for="FirstName"/> <!-- self closing tag -->

<span asp-validation-for="FirstName"/> <!-- self closing tag -->

And, insteand when I put the closing tags, I see the values being displayed.

<label asp-for="FirstName"></label>

<span asp-validation-for="FirstName"></span>

Generated HTML

<label for="FirstName">FirstName</label>
<span class="field-validation-error" data-valmsg-for="FirstName" data-valmsg-replace="true">
    <span id="FirstName-error" class="">The FirstName field is required</span>
</span>

What I want to know, what difference does it make? And, is self closing the paired tags a bad habit. If you know some article which talks about this design principle, please share with me, it would be much appreciated.

Amato answered 14/9, 2016 at 9:16 Comment(6)
Generally self-closing is fine, unless the client is using very old browser. or you mean self-closing the non-void elements?Labroid
Reference to self-closing non-void elements: #3558619Labroid
if non-void elements mean those which may not ever contain any content, e.g. input, img, br etc, then I didn't mean those. I am interested to know about the paired elements, the 2 in my example, where the generated HTML has the content placed within the opening & closing tags of label & span. So, is self-closing the paired tags like such is a serious offense?Amato
Example of non-void elements is div, span, textarea, etc. Those tags requires to have end tag.Labroid
...or elements not listed here: w3.org/TR/html-markup/syntax.html#void-elementLabroid
Thank #FrozenFire for clarifying it so well. Yes, then my question is for non-void elements. Now, as you say those tags requires end tag, is this by rule of thumb? do you know any reference for this rule.Amato
H
6

The reason they do not work is because the "MVC tag helpers do not change whether an element is self-closing or not" (from comments in issue #4475).

It is a known issue in ASP.NET and the plan is to show a warning in VS IDE under the scenario of a non-void element having an end tag (see issue #398). One of ASP.NET developers commented in issue #1302 that:

"this is the current design but we have a few issues (open and closed) on the behaviour"

Harragan answered 14/9, 2016 at 11:9 Comment(1)
Thanks for the info! I am surprised, how did you find such an accurate references. I tried to google it this afternoon but haven't reached relevant content. So decided to post it as a new question! Bingo, I got what I needed.Amato

© 2022 - 2024 — McMap. All rights reserved.