如何从Json文件中为模型属性添加自定义错误消息?
我将所有用于本地化的字符串存储在Json文件中。如下图所示如何从Json文件中为模型属性添加自定义错误消息?,json,asp.net-mvc,localization,Json,Asp.net Mvc,Localization,我将所有用于本地化的字符串存储在Json文件中。如下图所示 [ { "Key": "screen", "Value": "Screen No" }, { "Key": "go", "Value": "Go!" }, { "Key": "titlehome", "Value": "Search Particular Screan" }, { "Key": "home", "Value": "Home"
[
{
"Key": "screen",
"Value": "Screen No"
},
{
"Key": "go",
"Value": "Go!"
},
{
"Key": "titlehome",
"Value": "Search Particular Screan"
},
{
"Key": "home",
"Value": "Home"
}]
那么,如何在模型中访问这些字符串以进行数据表示。我创建了一个静态函数
Utils.value(“screen”)
,并返回该键的值 你好,纳辛巴哈
public class josnResult
{
public string Key { get; set; }
public string Value { get; set; }
}
var _listResult= Newtonsoft.Json.JsonConvert.DeserializeObject<List<josnResult>>(jsonValue);
josnResult _josnResult = _listResult.Where(t => t.Key == "screen").FirstOrDefault();
if (_josnResult != null)
{
var _val = _josnResult.Value;
}
公共类josnResult
{
公共字符串密钥{get;set;}
公共字符串值{get;set;}
}
var_listResult=Newtonsoft.Json.JsonConvert.DeserializeObject(jsonValue);
josnResult\u josnResult=\u listResult.Where(t=>t.Key==“screen”).FirstOrDefault();
如果(_josnResult!=null)
{
var _val=_josnResult.Value;
}
我做了这部分。我想显示来自这些json字符串的模型错误消息。我这样做如下[必需(ErrorMessage=Utils.Value(“requeredmessage”)]
但它给出了一个错误,即属性参数必须是常量表达式。请添加一些关于解决方案的说明。请创建模型(josnResult),然后反序列化字符串Newtonsoft.Json.JsonConvert.DeserializeObject(YOURJSONVALUE);在列表中,您可以轻松获得您的价值