开发者

Telerik MVC Combobox validation: change invalid value error message

@(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; }


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...

 [Required( ErrorMessageResourceType = typeof( Resources.ErrorStrings ),    ErrorMessageResourceName = "Required_SelectedFoo" )]
[RegularExpression("ExpressionforGUID")]
public string StringSelectedFoo { get; set; }
public GUID SelectedFoo{get{return (GUID)StringSelectedFoo;}}//have to do some sanitation work here

and on view you create AutoComplete or whatever for StringSelectedFoo instead of SelectedFoo


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 like RegularExpression validation attribute or something else to get the right validation message.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜