lidationAttribute和本地化的ErrorMessa

lidationAttribute和本地化的ErrorMessa

本文介绍了ASP.NET MVC:ValidationAttribute和本地化的ErrorMessage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有ValidationAttribute这样的:

I have ValidationAttribute like:

    public class Username : ValidationAttribute
 {
  public override bool IsValid(object value)
  {
   if (value == null)
    return false;

   return RegExp.Validate(RegExpLib.Username,value.ToString());
  }
 }

..并使用它是这样的:

..and using it like this:

    public class AccountSignIn
 {
  [Username(ErrorMessageResourceName ="txtUsername",ErrorMessageResourceType=typeof(SignIn))]
  public string Username { get; set; }

  public string Password { get; set; }

  public bool RememberMe { get; set; }

  public string ReturnUrl { get; set; }
 }

签到是在App_GlobalResources文件资源文件和txtUsername - 是在资源文件字符串名称。

SignIn is resource file at App_GlobalResources and "txtUsername" - is string name in resource file.

问题:
错误消息未示出。

Problem:Error messages are not shown.

问题:
如何设置错误消息,如果我在网站上几种语言。

Question:How to set error message if I have few languages on the website.

另一个信息:

我是从浏览才能访问 SignIn.txtUsername 或项目中的任何文件。从控制器

I am able access SignIn.txtUsername from Views or any file in the project. From controller

ModelState.AddModelError("Username", Resources.SignIn.txtUsername);

正常工作以及..我可以分配的ErrorMessage里面ValidationAttribute,却得到了第二确认后错误尝试...如果我摆检查仪这样的 -

works fine as well.. I can assign ErrorMessage inside ValidationAttribute, but got error after second validation try... if I place check like this -

   if(ErrorMessage != Resources.SignIn.txtUsername)
       ErrorMessage = Resources.SignIn.txtUsername;

我有错误后,我切换到另一种语言 - 的ErrorMessage只能被分配一次。

I have error after I switch to another language - ErrorMessage can be assigned only once.

请指点我怎样才能解决这个问题。

Please advice how I can solve it.

感谢您提前。

推荐答案

我遇到了一些问题,利用本地化错误消息和DataAnnotations,pretty你的情况多的是相同的。虽然我没有显示错误消息的空白之中结束了,我总是有一些例外抛出。解决方法对我来说是:

I had some problems with using localized error messages and DataAnnotations, pretty much identical to your situation. Although I didn't end up with blank being displayed as error messages, I always got some exceptions thrown. Solution for me was:


  1. 在App_GlobalResources文件/打开Errors.resx资源文件,改变它的属性生成操作:嵌入式的资源,自定义工具:PublicResXFile codeGenerator,自定义工具命名空间:资源(视觉工作室做一些奇怪的autogenerating这些,所以回来了,事情都OK检查)

  1. in App_GlobalResources/Errors.resx open the resource file, change it's properties to Build Action: Embedded Resource, Custom Tool: PublicResXFileCodeGenerator, Custom Tool Namespace: Resources (visual studio does something weird in autogenerating these, so check back again that things are ok)

更​​改资源文件的访问修饰符公共

Change the resource file's access modifier to Public

检查从生成(Errors.Designer.cs)的命名空间和访问修饰符是正确的code文件。

Check from the code file generated (Errors.Designer.cs) that the namespace and access modifier are correct.

试用

从Model类我的例子:

My example from the Model class:

[Required(ErrorMessageResourceType = typeof(Resources.Errors), ErrorMessageResourceName="ResponseMessageRequired")]
public string message { get; set; }

这篇关于ASP.NET MVC:ValidationAttribute和本地化的ErrorMessage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 07:58