加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – MVC:覆盖默认的ValidationMessage

发布时间:2020-12-15 23:32:07 所属栏目:asp.Net 来源:网络整理
导读:在MVC的世界我有这个视图模型… public class MyViewModel{[Required]public string FirstName{ get; set; } } …在我看来这样的事情… %= Html.ValidationSummary("Please correct the errors and try again.") %%= Html.TextBox("FirstName") %%= Html.Val
在MVC的世界我有这个视图模型…
public class MyViewModel{

[Required]
public string FirstName{ get; set; }    }

…在我看来这样的事情…

<%= Html.ValidationSummary("Please correct the errors and try again.") %>
<%= Html.TextBox("FirstName") %>
<%= Html.ValidationMessage("FirstName","*") %>

我的问题:如果我提交此表单而不提供名称,我会收到以下消息“需要的名字字段”

好.所以,我去把我的财产改成…

[DisplayName("First Name")]
[Required]
public string FirstName{ get; set; }

现在得到“名字字段是必需的”

到目前为止都不错

所以现在我想要错误信息显示“名字Blah Blah”.如何覆盖默认消息以显示DisplayName“Blah Blah”,wihtout注释所有的属性,类似于

[Required(ErrorMessage = "First Name Blah Blah")]

干杯,

ETFairfax

解决方法

public class GenericRequired: RequiredAttribute
{
    public GenericRequired()
    {
        this.ErrorMessage = "{0} Blah blah"; 
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读