首页 > 解决方案 > 字符串长度限制在.ne​​t core mvc的文本框中添加字符

问题描述

我正在尝试使用数据注释对字符串长度进行客户端验证,因此我已将 [Stringlength(100,ErrorMessage="Name Exceeds length 100")] 添加到模型属性中。

我想添加超过 100 个字符,它应该在 UI 上显示错误消息,但它不显示错误消息并且只接受 100 个字符。任何人都知道为什么字符串长度限制添加字符而不显示消息?

模型属性

[StringLength(100, ErrorMessage = "ABCD")] 公共字符串名称 { get; 放; }

MVC 控制-

@Html.TextBoxFor(x => x.Name)

文本框渲染 -

<input data-val="true" data-val-length="ABCD" data-val-length-max="100" id="Name" maxlength="100" name="Name" type="text" value=""/>

标签: asp.net-core-mvcstring-length

解决方案


您可以看到它maxlength="100"存在于呈现的文本框中,这会阻止您输入更多字符。你可以使用js来删除它。

<script>
$(function () {
    $("#Name").removeAttr("maxlength")
})
</script>

推荐阅读