MaxLength 属性不生成客户端验证属性
我对 ASP.NET MVC3 客户端验证有一个奇怪的问题。我有以下课程:
public class Instrument : BaseObject
{
public int Id { get; set; }
[Required(ErrorMessage = "Name is required.")]
[MaxLength(40, ErrorMessage = "Name cannot be longer than 40 characters.")]
public string Name { get; set; }
}
从我的角度来看:
<div class="editor-field">
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
这是我为该字段的文本框生成的 HTML:
<input class="text-box single-line" data-val="true" data-val-required="Name is required." id="Name" name="Name" type="text" value="">
没有 MaxLengthAttribute
的迹象,但其他一切似乎都正常。
有什么想法出了什么问题吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
尝试使用
[StringLength]
属性:这是出于验证目的。例如,如果您想在输入上设置 maxlength 属性,您可以将自定义数据注释元数据提供程序编写为 本文所示 并自定义 默认模板。
Try using the
[StringLength]
attribute:That's for validation purposes. If you want to set for example the maxlength attribute on the input you could write a custom data annotations metadata provider as shown in this post and customize the default templates.
我只是使用了jquery的一个片段来解决这个问题。
选择器查找具有 data-val-length-max 属性集的所有元素。这是 StringLength 验证属性将设置的属性。
Each 循环遍历这些匹配项,并解析出该属性的值并将其分配给应已设置的 mxlength 属性。
只需将其添加到您的文档准备功能中即可。
I just used a snippet of jquery to solve this problem.
The selector finds all of the elements that have a data-val-length-max attribute set. This is the attribute that the StringLength validation attribute will set.
The each loop loops through these matches and will parse out the value for this attribute and assign it to the mxlength property that should have been set.
Just add this to you document ready function and you are good to go.
MaxLengthAttribute
自 MVC 5.1 更新以来一直有效:更改说明MaxLengthAttribute
is working since MVC 5.1 update: change notes在MVC 4中
如果您想要输入类型文本中的最大长度?你可以 !
In MVC 4
If you want maxlenght in input type text ? You can !
支持 @Nick-Harrison 的回答:
我想知道 parseInt() 的用途是什么?我已经将其简化为这样,没有任何问题......
我会对尼克斯的答案发表评论,但还没有足够的代表。
Props to @Nick-Harrison for his answer:
I was wondering what the parseInt() is for there? I've simplified it to this with no problems...
I would have commented on Nicks answer but don't have enough rep yet.
我遇到了同样的问题,我可以通过在视图模型中实现 IValidatableObject 接口来解决它。
Razor 中的标记是...
这非常有效。如果我尝试使用 [StringLength] 来代替,那么呈现的 HTML 就不正确。验证应呈现为:
使用 StringLengthAttribute,呈现的 HTML 显示为 ValidationSummary,这是不正确的。有趣的是,当验证器失败时,提交仍然被阻止!
I had this same problem and I was able to solve it by implementing the IValidatableObject interface in my view model.
The markup in the Razor is then...
This works really well. If I attempt to use [StringLength] instead then the rendered HTML is just not correct. The validation should render as:
With the StringLengthAttribute the rendered HTML shows as a ValidationSummary which is not correct. The funny thing is that when the validator fails the submit is still blocked!
我知道我来晚了,但我终于找到了如何注册
MaxLengthAttribute
。首先我们需要一个验证器:
没什么特别的。在构造函数中,我们保存属性中的一些值。在
GetClientValidationRules
中,我们设置了一条规则。ValidationType = "length"
由框架映射到data-val-length
。rule.ValidationParameters["max"]
用于data-val-length-max
属性。现在,既然您有了一个验证器,您只需在
global.asax
中注册它:瞧,它就可以工作了。
I know I am very late to the party, but I finaly found out how we can register the
MaxLengthAttribute
.First we need a validator:
Nothing realy special. In the constructor we save some values from the attribute. In the
GetClientValidationRules
we set a rule.ValidationType = "length"
is mapped todata-val-length
by the framework.rule.ValidationParameters["max"]
is for thedata-val-length-max
attribute.Now since you have a validator, you only need to register it in
global.asax
:Et voila, it just works.
StringLength
效果很好,我这样使用它:或 只需使用
RegularExpression
而不使用 StringLength:但对我来说,上述方法在显示视图中给出了错误,原因我已经在数据库中拥有超过 25 个字符的 ProductName 字段
,所以最后我遇到了 这个< /a> 和这篇文章并尝试不使用这样的模型进行验证:
这解决了我的问题,您还可以使用 jquery 或使用 ModelState.AddModelError
希望能帮助某人。
StringLength
works great, i used it this way:or Just Use
RegularExpression
without StringLength:but for me above methods gave error in display view, cause i had already ProductName field in database which had more than 25 characters
so finally i came across this and this post and tried to validate without model like this:
this solved my issue, you can also do validation manually using jquery or using ModelState.AddModelError
hope helps someone.
我对 html 文档中具有 data-val-length-max 属性的所有输入(文本区域、输入等)尝试了此操作,并且它工作正常。
I tried this for all the inputs in my html document(textarea,inputs,etc) that had the data-val-length-max property and it works correctly.
这可以替换 MaxLength 和 MinLength
This can replace the MaxLength and the MinLength