Mvc的viewdata遍历问题

Seven-wz 2010-08-20 01:29:21
如下写法

首先一个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();这样的反射方法

求解...


...全文
827 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
momox 2010-08-26
  • 打赏
  • 举报
回复
终于找到了,看高人如何解决吧
http://blog.zhaojie.me/2010/05/asp-net-mvc-dynamic-view-model-binding-error-with-anonymous-types.html
Seven-wz 2010-08-22
  • 打赏
  • 举报
回复
实现方法
http://blog.csdn.net/wz361790599/archive/2010/08/22/5829590.aspx
gongsun 2010-08-20
  • 打赏
  • 举报
回复
4.0的dynamic 这么强...

越来越跟不上时代了...
wisdonlz 2010-08-20
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 webdiyer 的回复:]
如果你用.net framework 4.0的话很简单,用dynamic,象下面这样:

Controller中:

public ActionResult Index()
{
var result=from s in DataContext.Student where s.Sex==0 select new {Id=s.Id,Name=s.Name};
return V……
[/Quote]

webdiyer 2010-08-20
  • 打赏
  • 举报
回复
如果你用.net framework 4.0的话很简单,用dynamic,象下面这样:

Controller中:

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


View中:

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>
这样遍历:
<%foreach(var std in Model){%>
编号:<%:std.Id%></br>
姓名:<%:std.Name%>
<%}%>

不过你既然定义了Student类,那就没必要使用匿名对象,可以直接使用Student,这样的话不是.net framework 4.0也可以运行,象这样:

Controller中:

public ActionResult Index()
{
var result=(from s in DataContext.Student where s.Sex==0 select s).ToList();
return View(result);
}


View中:

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<List<Student>>" %>
这样遍历:
<%foreach(Student std in Model){%>
编号:<%:std.Id%></br>
姓名:<%:std.Name%>
<%}%>
wuyq11 2010-08-20
  • 打赏
  • 举报
回复
直接View中foreach ViewData保存的List生成数组
使用保存JSON到ViewData,each遍历
宝_爸 2010-08-20
  • 打赏
  • 举报
回复
匿名类只能用于函数内部。没法跨函数的。反射倒是一种办法。
要不就不使用匿名函数

Controller:

[HandleError]
public class HomeController : Controller
{
public ActionResult Index()
{
ViewData["Message"] = "Welcome to ASP.NET MVC!";
List<Student> array = new List<Student>();
array.Add(new Student(){Id = 10, Name = "aaa"});
array.Add(new Student() { Id = 11, Name = "bbb" });
array.Add(new Student() { Id = 12, Name = "ccc" });
array.Add(new Student() { Id = 13, Name = "dddd" });

ViewData["Array"] = from s in array select s;

return View();
}


View:

<p> <% foreach (var one in (IEnumerable<MvcApplication2.Controllers.Student>)ViewData["Array"]) { %></p>
<h1> <%=one.Id %> - <%=one.Name %> </h1>
<% } %>
Seven-wz 2010-08-20
  • 打赏
  • 举报
回复
谢谢大家的回答....可能事实就像1楼所说的 只能反射了..

用匿名类是因为比如Student这个类有17个属性
在控制器中我只需要两个呈现到视图,别的不想要,才选择匿名类...

若通过已定义Student去遍历,那所有具有Id Name的类都可以当作foreach的类型去遍历,
而如果linq查找返回的匿名类 Id不叫Id了,叫StuId,Name叫StuName 这样Student就无法遍历了...

看了大家的回答,我思考后可以扩展如下的方法
foreach(typeof(new{Id,Name}) stu in (IEnumerator)ViewMode["StuList"]) 其实还是反射...

3楼大哥意思可能是转到客户端处理...




62,041

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧