場景: - 我正在開發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 –