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

故事扮演 提交于 2019-12-19 09:00:01

问题


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.


回答1:


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"



来源:https://stackoverflow.com/questions/39486703/tag-helpers-self-closing-html-tags-is-a-bad-habit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!