【发布时间】:2013-12-30 09:55:24
【问题描述】:
我想迭代从foreach(var item in Model) 返回的项目。喜欢for(int i=0; i <item.length; i++),然后为模型返回的项目编写单独的html代码。
不幸的是,我似乎无法访问item.length,因为该属性不存在。有谁知道我如何遍历每个模型项,以便节省编写大量不必要的 HTML 的时间?
因此,请提供更多上下文来代替不清楚的问题:
假设我的模型是
public class RegisterViewModel
{
public string Name { get; set; }
public string Email { get; set; }
public string Password { get; set; }
}
现在我使用数据库调用来访问模型数据并将其解析到视图中
RegisterViewModel model= db.RegisterViewModel.Where(data => data.Name == User.Name);
现在我通过view(model)将此模型传递给视图
现在要显示所有学生的数据,我可以使用
model IEnumerable<Project.Models.RegisterViewModel>
@foreach(item in Model) {
@for(int i=0; i<item.Length;i++) { \\this is where I would like to iterate though the item
if(item[i]!= null) // using a for loop would avoid me having to write this if condition multiple times.
<div>@item[i].value</div>
}
}
【问题讨论】:
-
什么是
item?你能显示Model的代码吗? -
我确信我绝对可以对不赞成票使用一点解释。我相信这个问题是非常有效的,因为我在服务器端几乎找不到任何东西来迭代这些元素。我认为的唯一方法是将对象解析到客户端,其余部分使用 JavaScript。
标签: html asp.net-mvc-4 view model