【问题标题】:Custom client message自定义客户端消息
【发布时间】:2011-06-20 07:17:46
【问题描述】:

我想使用属性在我的模型上本地化我的错误消息。 我创建了App_GlobalResources 并添加了 ResourceMessages。我添加了 PropertyValueInvalid- 我的错误信息

在 Global.asax 我添加了

DefaultModelBinder.ResourceClassKey = "ResorceMessages";
ValidationExtensions.ResourceClassKey = "ResorceMessages";

如果我没有启用客户端验证,这项工作没有。但是如果我有

<script src="<%: Url.Content("~/Scripts/jquery.validate.min.js") %>" type="text/javascript"></script>
<script src="<%: Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js") %>" type="text/javascript"></script>

包含在我的母版页中,我会收到标准验证消息。

我该如何解决这个问题?


[Required(ErrorMessageResourceName = "Required", ErrorMessageResourceType = typeof(ResursPoruke))]

[Remote("ProvjeraGodine", "RemoteValidacije", "Godina nije validna")]

[Display(ResourceType = typeof(Resource), Name = "OsnovniPodaci_Godina")]

public int Godina { get; set; }

PropertyValueInvalid    Vrijednost "{0}" nije ispravna za polje: {1}

我应该得到类似的错误;

Vrijednost "dkdkdkdkdk" nije ispravna za polje: Godina 

但我得到错误

The field Godina proizvodnje must be a number.  

如果我打开 jquery 验证,我会收到我的消息。 如果我有 jquery 验证,如何获得验证消息?

【问题讨论】:

    标签: c# javascript jquery asp.net-mvc-3 jquery-validate


    【解决方案1】:

    您说您添加了 ResourceMessages 资源,但您的类键指向不同的字符串 ResorceMessages。是不是笔误?

    DefaultModelBinder.ResourceClassKey = "ResorceMessages"; <<----
    ValidationExtensions.ResourceClassKey = "ResorceMessages"; <<----
    

    【讨论】:

      【解决方案2】:

      默认验证消息与密钥 InvalidPropertyValue 一起存储(而默认模型绑定器错误消息与 PropertyValueInvalid"). 但是,如果已经定义了错误消息,则不使用此默认消息。

      【讨论】:

      • 我找不到有关在不同情况下使用的密钥的任何文档。你在哪里找到这些钥匙的?使用反射?
      • AFAI记得我查了MVC3的源代码。如果我使用 MVC,我会打开它。我从这里下载的:aspnet.codeplex.com/releases/view/58781
      猜你喜欢
      • 2012-01-21
      • 2015-04-17
      • 1970-01-01
      • 1970-01-01
      • 2019-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-12
      相关资源
      最近更新 更多