【问题标题】:String Length restricting to add characters in textbox in .net core mvc字符串长度限制在.ne​​t core mvc的文本框中添加字符
【发布时间】:2020-08-29 21:00:19
【问题描述】:

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

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

模型属性

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

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-mvc string-length


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2010-12-28
      • 1970-01-01
      • 1970-01-01
      • 2017-06-13
      • 1970-01-01
      • 1970-01-01
      • 2012-07-01
      • 1970-01-01
      • 2011-01-09
      相关资源
      最近更新 更多