C# 在StringTemplate中引用ExpandooObject的正确方式是什么?
我从控制器中的ExpandooObject中获取了一个ViewData值,如下所示:C# 在StringTemplate中引用ExpandooObject的正确方式是什么?,c#,json,asp.net-mvc-2,stringtemplate,expandoobject,C#,Json,Asp.net Mvc 2,Stringtemplate,Expandoobject,我从控制器中的ExpandooObject中获取了一个ViewData值,如下所示: public ActionResult CountryData() { string json = "{\"Countries\":[{\"CountryId\":1,\"Country\":\"USA\",\"Description\":\"North America\"},{\"CountryId\":2,\"Country\":\"Russia\",\"Description\":\"Europ
public ActionResult CountryData()
{
string json = "{\"Countries\":[{\"CountryId\":1,\"Country\":\"USA\",\"Description\":\"North America\"},{\"CountryId\":2,\"Country\":\"Russia\",\"Description\":\"Europe\"},{\"CountryId\":3,\"Country\":\"Argentina\",\"Description\":\"South America\"}]}";
dynamic values = deserializeToDictionary(json);
ViewData[key2] = values[key2]; //the key is "Countries"
return View();
}
private Dictionary<string, object> deserializeToDictionary(string JsonString)
{
dynamic dataObj = new ExpandoObject();
var values = dataObj as IDictionary<string, object>;
values = JsonConvert.DeserializeObject<ExpandoObject>(JsonString);
Dictionary<string, object> values2 = new Dictionary<string, object>();
foreach (KeyValuePair<string, object> d in values)
{
if (d.Value.GetType().FullName.Contains("Newtonsoft.Json.Linq.JObject"))
{
values2.Add(d.Key, deserializeToDictionary(d.Value.ToString()));
}
else
{
values2.Add(d.Key, d.Value);
}
}
return values2;
}
public ActionResult CountryData()
{
字符串json=“{\'Countries\”:[{\'CountryId\”:1,“Country\”:“USA\”,“Description\”:“North America\”,{\'CountryId\”:2,“Country\”:“Country\”:“Russia\”,“Description\”:“Europe\”,{\'CountryId\”:3,“Country\”:“Country\”:“Argentina\”,“Description\”:“South America\”);
动态值=反序列化字典(json);
ViewData[key2]=值[key2];//键为“国家”
返回视图();
}
私有字典反序列化字典(字符串JsonString)
{
动态数据对象=新的ExpandooObject();
var值=作为IDictionary的dataObj;
values=JsonConvert.DeserializeObject(JsonString);
字典值2=新字典();
foreach(值中的KeyValuePair d)
{
if(d.Value.GetType().FullName.Contains(“Newtonsoft.Json.Linq.JObject”))
{
values2.Add(d.Key,反序列化字典(d.Value.ToString());
}
其他的
{
值2.添加(d.键,d.值);
}
}
返回值2;
}
注意:代码已从中删除
我尝试了几种在模板中引用它的方法,但似乎无法使其工作
(a) 我尝试了常规的字典查找:
<select name="Countries" >
<option value=""></option>
$Countries:{Country
<option title="$Country["Capital"]$" $if(Country["Selected"]="")$ selected="selected" $endif$ value="$Country["CountryId"]$">$Country["Name"]$</option>
}$
</select>
$国家:{国家
$Country[“Name”]$
}$
及
$国家:{国家
$Country[1]$
}$
(b) 也尝试了引用强类型值/数据传输对象的常规方法
<select name="Countries" >
<option value=""></option>
$Countries:{Country
<option title="$Country.Capital$" $if(Country.Selected="")$ selected="selected" $endif$ value="$Country.CountryId$">$Country.Name$</option>
}$
</select>
$国家:{国家
$Country.Name$
}$
两个都不起作用。那么它是如何做到的呢
注意:我不希望使用带有硬编码属性名的强类型对象
谢谢
<select name="Countries" >
<option value=""></option>
$Countries:{Country
<option title="$Country.Capital$" $if(Country.Selected="")$ selected="selected" $endif$ value="$Country.CountryId$">$Country.Name$</option>
}$
</select>