场景: - 我正在开发MVC 4应用程序,该网站将以多种语言运行,并将托管在Azure上。 对于本地化我们依赖于数据库而不是资源包方式。MVC 4从数据库本地化验证消息
问题: - 我想在运行时自定义错误消息,我想通过数据库本地化消息。
我试图通过反射改变属性值,但它没有奏效。
代码: -
//Model
public class Home
{
[Required(ErrorMessage = "Hard coded error msg")]
public string LogoutLabel { get; set; }
}
//On controller
public ActionResult Index()
{
Home homeData = new Home();
foreach (PropertyInfo prop in homeData.GetType().GetProperties())
{
foreach (Attribute attribute in prop.GetCustomAttributes(false))
{
RequiredAttribute rerd = attribute as RequiredAttribute;
if (rerd != null)
{
rerd.ErrorMessage = "dynamic message";
}
}
}
return View(homeData);
}
在客户端验证时需要把它显示我旧消息“硬编码错误味精”。 如果我们不想使用资源绑定方法,请建议如何自定义这个方法
我这里udescribed我的方法: http://stackoverflow.com/questions/19398691/mvc-localisation-from-the-database-that- cover-all-messages-required-displayna –