MVC:覆盖默认ValidationMessage [英] MVC: Override default ValidationMessage
问题描述
在MVC的世界里,我有这个视图模型......
公共类MyViewModel {[需要]
公共字符串名字{获得;组; }}
...而这样的事情在我看来......
<%= Html.ValidationSummary(请改正错误,然后再试一次。)%>
<%= Html.TextBox(名字)%>
&所述;%= Html.ValidationMessage(姓,*)%>
我的问题:如果我提交此表没有提供一个名字,我得到了以下信息:名字字段是必需的
确定。于是,我去和我的属性更改为...
[DisplayName的(名)]
[需要]
公共字符串名字{获得;组; }
..现在得到第一个名字字段是必需的
所有好为止。
所以现在我想要的错误消息显示名字等等等等。我怎么能覆盖默认的消息显示显示名称+等等等等,wihtout注释所有的东西的属性,如
[必需(的ErrorMessage =名字等等等等)]
干杯,
ETFairfax
公共类GenericRequired:RequiredAttribute标签
{
公共GenericRequired()
{
this.ErrorMessage ={0}等等等等;
}
}
In the world of MVC I have this view model...
public class MyViewModel{
[Required]
public string FirstName{ get; set; } }
...and this sort of thing in my view...
<%= Html.ValidationSummary("Please correct the errors and try again.") %>
<%= Html.TextBox("FirstName") %>
<%= Html.ValidationMessage("FirstName", "*") %>
My question: If I submit this form without supplying a name, I get the following message "The FirstName field is required"
OK. So, I go and change my property to...
[DisplayName("First Name")]
[Required]
public string FirstName{ get; set; }
..and now get "The First Name field is required"
All good so far.
So now I want the error message to display "First Name Blah Blah". How can I override the default message to display DisplayName + " Blah Blah", wihtout annotating all the properties with something like
[Required(ErrorMessage = "First Name Blah Blah")]
Cheers,
ETFairfax
public class GenericRequired: RequiredAttribute
{
public GenericRequired()
{
this.ErrorMessage = "{0} Blah blah";
}
}
这篇关于MVC:覆盖默认ValidationMessage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!