【发布时间】:2012-12-20 07:21:01
【问题描述】:
场景:- 我正在开发 MVC 4 应用程序,该网站将以多种语言运行,并将托管在 Azure 上。 对于本地化,我们依赖于数据库而不是资源包方法。
问题:- 我想在运行时自定义错误消息,我想通过数据库本地化消息。
我曾尝试通过反射来更改属性值,但没有成功。
代码:-
//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);
}
在客户端进行验证时,它会向我显示旧消息“硬编码错误消息”。 如果我们不想使用资源包方法,请建议如何定制它
【问题讨论】:
-
我在这里描述了我的方法:stackoverflow.com/questions/19398691/…
标签: asp.net-mvc validation asp.net-mvc-4