Telerik MVC Combobox 验证:更改无效值错误消息
@(Html.Telerik().ComboBoxFor( x => x.SelectedFoo )
.DataBinding( x => x.Ajax().Select( "_List", "Foo" ) )
.AutoFill( true )
.HighlightFirstMatch( true )
.Filterable( x => x.FilterMode( AutoCompleteFilterMode.StartsWith ) )
)
@Html.ValidationMessageFor( x => x.SelectedFoo )
好吧,所以我正在使用 Telerik 的 ASP.NET MVC 组合框组件,但我找不到在输入无效值时设置/更改(也本地化)错误消息的位置。
默认错误消息是
值“asd”对于 SelectedFoo 无效
引发此错误是因为 "asd"
不是组合框允许值集的一部分。
如果可能的话,我想使用 DataAnnotations 来做到这一点。
这就是我目前所拥有的:
[Required( ErrorMessageResourceType = typeof( Resources.ErrorStrings ),
ErrorMessageResourceName = "Required_SelectedFoo" )]
public Guid? SelectedFoo { get; set; }
@(Html.Telerik().ComboBoxFor( x => x.SelectedFoo )
.DataBinding( x => x.Ajax().Select( "_List", "Foo" ) )
.AutoFill( true )
.HighlightFirstMatch( true )
.Filterable( x => x.FilterMode( AutoCompleteFilterMode.StartsWith ) )
)
@Html.ValidationMessageFor( x => x.SelectedFoo )
Alright, so I'm using telerik's combo box component for ASP.NET MVC, and I can't find where to set/change (also localize) the error message when an invalid value is entered.
The default error message is
The value 'asd' is not valid for SelectedFoo
This error is thrown because "asd"
is not part of the set of allowed values for the combo box.
I'd like to do this using DataAnnotations, if possible.
This is what I currently have:
[Required( ErrorMessageResourceType = typeof( Resources.ErrorStrings ),
ErrorMessageResourceName = "Required_SelectedFoo" )]
public Guid? SelectedFoo { get; set; }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这很奇怪,但可以解释。 SelectedFoo 是 GUID 类型。当框架尝试将 asd 强制转换为 GUID 类型时,它会抛出异常,并将其添加到 modelstate 字典中,并且所有这一切都发生在调用验证属性之前。因此,在这种情况下,您可能无法以简单且合理的方式改变它的方式。但是,您可以做的是更改您的模型,例如...
并在视图中为 StringSelectedFoo 而不是 SelectedFoo 创建 AutoComplete 或其他内容
This is weird but can be explained. SelectedFoo is of typte GUID. when framework tries to caste asd to type GUID it throws and exception which is added to the modelstate dictionary and all this happens before invocation of validation attributes. So, in this scenario you probably can't change the way it is in an easy and plausible fashion. What you can do however is to change your model like...
and on view you create AutoComplete or whatever for StringSelectedFoo instead of SelectedFoo
你做的一切都是对的。
但是当询问无效值时,
所需
验证有效。Required
没有任何问题,您需要使用诸如RegularExpression
验证属性或其他内容来获取正确的验证消息。You did everything right.
But when invalid value intered the
Requried
validation is valid.There is nothing wrong with
Required
you need to but something likeRegularExpression
validation attribute or something else to get the right validation message.