1、将匿名类型赋给ViewBag然后通过反射读取
@foreach (var i in @ViewBag.Ages) { <div style="border:1px solid black;">@i.GetType().GetProperty("Age").GetValue(i,null)</div> }
2、借助JSON.NET,不过在RAZOR视图中失去了强类型的感知
List<Test> source = new List<Test> { new Test{Age=20,Name="阿猫"}, new Test{Age=30,Name="阿狗"}, new Test{Age=1,Name="王二麻子"} }; var r = source.Select(t => new { Age = t.Age * 5 }).ToList(); return View(JsonConvert.DeserializeObject(JsonConvert.SerializeObject(r)));
时间: 2024-10-31 22:22:07