Mvc的viewdata遍历问题

Seven-wz Seven-wz 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
如下写法

首先一个Student的class

public class Student
{
public Int32 Id { get; set; }
public String Name{ get; set; }
}

然后在一个Index视图控制器中 如下:

public ActionResult Index()
{
var result=from s in DataContext.Student where s.Sex==0 select new {Id=s.Id,Name=s.Name};

//foreach(var obj in result)
//{ Response.Write(obj.Id); }这里用foreach是可以看到匿名类的属性的

ViewData["stuList"]=result; //至于写成 result.GetEnumerator() 或result.ToList<Stu>()可以随意

return View();
}

问题出现,在Index视图中如何foreach获取匿名类的属性...

排除foreach然后Obj.GetType().GetProperty("Id").GetValue(obj,null).ToString();这样的反射方法

求解...


...全文

相关推荐

评论 9
收藏 已收藏
分享
返回顶部
Seven-wz
.NET社区 62,271 成员 668,968 内容