ASP.NET Core验证标记帮助程序奇怪的行为

时间:2022-11-01 11:04:34

Today I noticed strange behaviour of ASP.NET Core taghelper when testing validation messages.

今天我在测试验证消息时注意到了ASP.NET Core taghelper的奇怪行为。

I have no jquery-validate and jquery-validate-unobtrusive in place.

我没有jquery-validate和jquery-validate-unobtrusive。

This works fine:

这很好用:

<span asp-validation-for="Name" class="text-danger"></span>

This is not working:

这不起作用:

<span asp-validation-for="Name" class="text-danger" />

Any idea why?! Is this a bug?

知道为什么吗?!这是一个错误吗?

Thanks for your inputs!

感谢您的投入!

1 个解决方案

#1


1  

@Jonathan is correct - this an invalid element to be self-closing. Making a tag self-closing indicates that it is a void element - meaning the tag has no content inside of it. Span tags totally have content!

@Jonathan是正确的 - 这是一个自动关闭的无效元素。使标签自动关闭表示它是一个void元素 - 意味着标签内部没有内容。 Span标签完全有内容!

However, the reason it doesn't work in Core is because what happens on this line. With a self-closing tag, the TagMode property on the output variable is set to SelfClosing, and the TagBuilder isn't going to put any of the message content in to it (because it is a void element which has no content).

但是,它在Core中不起作用的原因是因为在这一行上发生了什么。使用自闭合标记,输出变量上的TagMode属性设置为SelfClosing,并且TagBuilder不会将任何消息内容放入其中(因为它是一个没有内容的void元素)。

With a </span> at the end, the mode changes to StartTagAndEndTag and content is properly added.

最后使用 ,模式将更改为StartTagAndEndTag并正确添加内容。

#1


1  

@Jonathan is correct - this an invalid element to be self-closing. Making a tag self-closing indicates that it is a void element - meaning the tag has no content inside of it. Span tags totally have content!

@Jonathan是正确的 - 这是一个自动关闭的无效元素。使标签自动关闭表示它是一个void元素 - 意味着标签内部没有内容。 Span标签完全有内容!

However, the reason it doesn't work in Core is because what happens on this line. With a self-closing tag, the TagMode property on the output variable is set to SelfClosing, and the TagBuilder isn't going to put any of the message content in to it (because it is a void element which has no content).

但是,它在Core中不起作用的原因是因为在这一行上发生了什么。使用自闭合标记,输出变量上的TagMode属性设置为SelfClosing,并且TagBuilder不会将任何消息内容放入其中(因为它是一个没有内容的void元素)。

With a </span> at the end, the mode changes to StartTagAndEndTag and content is properly added.

最后使用 ,模式将更改为StartTagAndEndTag并正确添加内容。