c# - 在 MVC View 中使用 Razor 循环模型属性

标签 c# asp.net-mvc razor model viewdata

我的模型有很多属性

public class Task {
    public string Key { get; set; }
    public string Title { get; set; }
    public string Description { get; set; }
    ....
}

我需要在 View 中创建一个表来列出任务的所有属性。

已经问了同样的问题,但我不想使用 ViewData:Looping through view Model properties in a View

有什么想法吗?

最佳答案

这对你有用吗?

@foreach(var task in Model) {
    <tr>
    @foreach(var property in typeof(Task).GetProperties()) {
        <th>@(property.GetValue(task, null))</th>
    }
    </tr>
}

关于c# - 在 MVC View 中使用 Razor 循环模型属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37058536/

相关文章:

javascript - 来自不同页面时如何显示/隐藏单个元素?

asp.net-mvc - 如何在没有提示的情况下为 Google Chrome 启用自动登录

asp.net-core - 不同的基本路径取决于开发/生产环境

c# - 从数据存储中检索 key (更新和删除实体)

c# - 类型 'System.String[]' 不支持比较运算符

c# - Web API授权access_token验证

c# - 如何在 ASP.NET MVC4 中渲染特定模型的 View ?

c# - 如何在 bootstrap alert -bootstrap MVC 中插入换行符

c# - 我有一个需要打包的 C# WPF 应用程序。有什么建议么?

c# - IProgress<T> 未触发