0

我在国际化我的代码时遇到了问题。除了这部分代码外,它在我的网站上几乎所有地方都很好用。

[DataType(DataType.Password)]
    [Display(ResourceType = typeof(strings), Name = "BevestigWachtwoord")]
    private CultureInfo resourceCulture;
    [Compare("Password", ErrorMessage = ResourceManager.GetString("PassMismatch",resourceCulture))]
    public string ConfirmPassword { get; set; }

错误(需要对象引用......)在 ResourceManager.GetString("PassMismatch",resourceCulture))] 如果我尝试 ErrorMessage=strings.PassMismatch (其中字符串是我的资源),我会得到同样的错误文件)当我只填写一个字符串时,它确实有效。我有同样的显示问题,但我通过这样做解决了这个问题

[Display(ResourceType = typeof(strings), Name = "Email")]

我可以在这里尝试类似的东西吗?

编辑:我的错误

错误 13 非静态字段、方法或属性“MvcApplication2.Models.RegisterModel.resourceCulture”C:\Users\stuart\documents\visual studio 2010\Projects\MvcApplication2\MvcApplication2\Models\AccountModels 需要对象引用。 cs 74 86 MvcApplication2 错误 14 非静态字段、方法或属性需要对象引用 'System.Resources.ResourceManager.GetString(string, System.Globalization.CultureInfo)' C:\Users\stuart\documents\visual studio 2010\Projects\MvcApplication2\MvcApplication2\Models\AccountModels.cs 74 45 MvcApplication2 错误 11 分配给“MvcApplication2.Models.RegisterModel.error”的表达式必须是常量 C:\Users\stuart\documents\visual studio 2010\Projects \MvcApplication2\MvcApplication2\Models\AccountModels。cs 67 30 MvcApplication2 错误 12 属性或索引器“MvcApplication2.strings.PassMismatch”无法在此上下文中使用,因为它缺少获取访问器 C:\Users\stuart\documents\visual studio 2010\Projects\MvcApplication2\MvcApplication2\Models\ AccountModels.cs 67 30 MvcApplication2

对于其他评论,我正在尝试类似 ErrorMessage = resourceCulture ResourceManager.GetString("PassMismatch",Thread.CurrentThread.CurrentUICulture))]

但只是没有工作

4

2 回答 2

0

即使您尝试为 ResourceManager 传递静态引用并获取 CurrentCulture:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.Resources;
using System.Globalization;

namespace QMVC.ViewModel.Home
{
  public class TResViewModel
  {
    private static ResourceManager r = QMVC.Properties.Resources.ResourceManager;

    [Required(ErrorMessage = r.GetString("Test", CultureInfo.CurrentUICulture))]
    public string Test { get; set; }
  }
}

您将在编译时遇到错误: “属性参数必须是属性参数类型的常量表达式、类型表达式或数组创建表达式”

换句话说:您不能动态地为属性的参数设置值,因为必须在编译时解析属性参数(这是有道理的)。

最好(简单)的方法可能是使用其他参数作为验证属性,ErrorMessageResourceType 和 ErrorMessageResourceName:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.Resources;
using System.Globalization;

namespace QMVC.ViewModel.Home
{
  public class TResViewModel
  {  
    [Required(ErrorMessageResourceType = typeof(QMVC.Properties.Resources), ErrorMessageResourceName = "Test")]
    public string Test2 { get; set; }

      }
    }

在验证过程中,将使用您的资源类型、给定键和 CurrentCulture 创建 ErrorMessage。

问候。

于 2012-06-08T14:26:27.587 回答
0

替换这一行:

[Compare("Password", ErrorMessage = ResourceManager.GetString("PassMismatch",resourceCulture))]

有了这条线:

[Compare("Password", ErrorMessageResourceType = typeof(strings), ErrorMessageResourceName = "PassMismatch")]

并确保您已将 PassMismatch 字符串添加到映射到您的strings资源类型的资源文件中。

于 2012-06-08T14:42:33.203 回答