mvc 传递匿名对象

时间:2023-11-17 22:31:08

Controller代码:

public ActionResult TupleTest()
{
LinqDBEntities db = new LinqDBEntities();
dynamic data = db.StuInfo.ToList().Select(s => Tuple.Create(s.StuNum, s.StuName, s.StuAge,s.StuSex, s.ClassID));
ViewData["data"] = data;
return View();
}

在Controller中把要传递的数据通过Tuple.Create封装数据

View代码:

mvc 传递匿名对象
@{            dynamic dd = ViewData["data"];
foreach (var item in dd)
{
Response.Write(item.Item1+"-"+item.Item2+"-"+item.Item3+"-"+item.Item4+"-"+item.Item5+"<br>");
}
}