問題描述
我將 TagHelpers
合并到我的 MVC vNext 項目中,我意識到當我自行關閉 HTML 標簽時它們不起作用.
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>
生成的 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.
推薦答案
它們不起作用的原因是因為MVC 標簽助手不會改變元素是否自動關閉"(來自問題中的評論 #4475).
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).
這是 ASP.NET 中的一個已知問題,計劃是在具有結束標記的非空元素的情況下在 VS IDE 中顯示警告(請參閱問題 #398).一位 ASP.NET 開發人員在問題 #1302 中評論說:
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"
這篇關于Tag Helpers - 自關閉 HTML 標簽是個壞習慣?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!