asp.net - 如何控制模型验证错误的显示语言

标签 asp.net asp.net-mvc localization

在 ASP.NET MVC 应用程序中,更改 Thread.CurrentThread.CurrentCulture[UI] 可以更改 MVC 从资源中选取消息的方式。在我创建的用于演示该问题的示例应用程序中,有两个资源文件 - 用于英语消息的 Res.resx 和用于西类牙语消息的 Res.es.resx。

但是,模型验证产生的错误消息始终以英语显示。

我的问题是,如何控制模型验证错误消息的显示语言?

下面是我编写的示例应用程序的一部分(基于默认的 ASP.NET MVC 应用程序)来演示此问题。

浏览器中的屏幕截图:

https://dl.dropboxusercontent.com/u/4453002/SO_LanguageOfValidation.png

ViewModel 和 Controller - HomeController.cs:

using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Threading;
using System.Web.Mvc;

namespace SO_ValidationMessageInEnglish.Controllers {

  /// <summary>
  /// A very basic view model.
  /// </summary>
  public class ViewModel {

    [Display(Name = "Message", ResourceType = typeof(Res))]
    [DisplayName("Message")]
    [Required(AllowEmptyStrings = false, ErrorMessageResourceName = "MessageRequired", ErrorMessageResourceType = typeof(Res))]
    public string Message { get; set; }

    public string Language { get; set; }
  }

  public class HomeController : Controller {

    public ActionResult Index(string language = "en") {
      Thread.CurrentThread.CurrentCulture = new CultureInfo(language);
      Thread.CurrentThread.CurrentUICulture = new CultureInfo(language);
      return View();
    }

    [HttpPost]
    [ActionName("Index")]
    public ActionResult IndexPost(ViewModel foo) {
      Thread.CurrentThread.CurrentCulture = new CultureInfo(foo.Language ?? "en");
      Thread.CurrentThread.CurrentUICulture = new CultureInfo(foo.Language ?? "en");
      return View(foo);
    }

  }
}

查看 - Index.cshtml :

@model SO_ValidationMessageInEnglish.Controllers.ViewModel
@using SO_ValidationMessageInEnglish
@{ ViewBag.Title = Res.Title; }
@Res.CurrentMessage:<br />
<h2>@((Model != null) ? Model.Message : Res.Default)</h2>
<p />    
@using (Html.BeginForm("Index", "Home", FormMethod.Post, null)) {        
    @Html.LabelFor(m => m.Message)    
    @Html.TextBoxFor(m => m.Message)
    @Html.HiddenFor(m => m.Language)
    @Html.ValidationMessageFor(m => m.Message)
    <input type="submit" value="@Res.Submit" />
}

最佳答案

我也遇到了同样的问题。当模型绑定(bind)器具有无效数据时,它会在 ActionFilter 之前运行。

我不喜欢建议的解决方案,因为弄乱路由不是我的首选解决方案。监听 Application_AcquireRequestState 是有问题的,因为此事件会针对每个请求触发,而不仅仅是针对将路由到 MVC Controller 的请求。

我最终编写了一个 IControllerFactory 的自定义实现,它在内部使用 DefaultControllerFactory 并在 CreateController 方法中执行本地化代码。
这也不理想,希望它有所帮助。

  public class PluggableControllerFactory : IControllerFactory {

    private readonly IControllerFactory innerControllerFactory;

    public PluggableControllerFactory() {
      innerControllerFactory = new DefaultControllerFactory();
    }

    public IController CreateController(System.Web.Routing.RequestContext requestContext, string controllerName) {
      // Run your culture localization here

      return innerControllerFactory.CreateController(requestContext, controllerName);
    }

    public System.Web.SessionState.SessionStateBehavior GetControllerSessionBehavior(System.Web.Routing.RequestContext requestContext, string controllerName) {
      return innerControllerFactory.GetControllerSessionBehavior(requestContext, controllerName);
    }

    public void ReleaseController(IController controller) {
      innerControllerFactory.ReleaseController(controller);
    }

  }
}

关于asp.net - 如何控制模型验证错误的显示语言,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17639974/

相关文章:

angularjs - Angular2 本地化 URL,命名路由

asp.net - Razor View 中的 div 颜色

c# - 如何使用 jQuery 或 JavaScript 或服务器端代码获取客户端(用户)机器名称(计算机名称)。 (审查可能的方式)

c# - 如何从 1 个特定页面的布局中删除 HTML 元素?

asp.net - ASP MVC外键提交问题

asp.net-mvc - ASP.NET MVC - 本地化路线

c# - .net core 中的通用列表不包含 AsQueryable()

c# - 如何将 TextBox 输入传递给 asp.net 中的 gridview?

c# - 使用自动映射器映射两个数组返回空数组

jsp - 如何本地化 Java 中的数字系统,例如印地语还是马拉地语?