c# - 如何使用动态模型创建动态表

标签 c# html asp.net-mvc-4 razor html-table

我想制作一个表,这样我可以加载一个类到其中,该表就会自动生成。

这就是我正在寻找的,但我不知道如何实现这一点:

@model dynamic
<table>
<thead>
    <tr>
    @foreach(var record in model.results)
    {
        <th>@Html.labelfor(item.indexof(...))</th>
    }

    </tr>
</thead>
@if (Model.Result.Count() > 0)
{
    <tbody>
        @for(int i = 0; i < Model.Result.Count(); i++)
        {
            <tr>
                @foreach (var item in Model.Result)
                {  
                    <td>@item.indexOf(...)</td>
                }
            </tr>
        }
    </tbody>
}
else
{
    <p class="errordata">No data found...</p>
}

这是我的 View 模型:

public class issueModel
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Color { get; set; }
    public string FontColor { get; set; }
    public int Count { get; set; }
    public string Description { get; set; }
    public string Abbreviation { get; set; }
    public string icon { get; set; }
    public List<dynamic> Result { get; set; }
}

在“列表结果”中可以不同,这是结果中可能出现的示例类:

public partial class someClass
{
    [Display(Name = "PID_1")]
    public int? someProp_1{ get; set; }

    [Display(Name = "PID_2")]
    public int? someProp_2{ get; set; }

    [StringLength(255)]
    public string PermName1 { get; set; }

    [StringLength(255)]
    public string PermName2 { get; set; }

    public string description{ get; set; }

    public int? Prioriteit { get; set; }

    [StringLength(50)]
    public string Login { get; set; }

    public int id{ get; set; }
}

我尝试在动态项目上使用 .indexOf,但这不起作用。有某种方法可以做到这一点吗?或者我是否必须使用单独的表制作单独的 View ?

最佳答案

对于这种情况,您需要反射(reflection)。您应该构建一个新的 Html Helper。该助手会迭代模型的所有公共(public)属性,并使用该信息创建一个表,然后您只需在 View 中调用助手:

帮助类:

namespace System.Web.Mvc
{
    public static class TableHelperExtensions
    {
        public static string BuildTr(object _obj)
        {
            var properties = _obj.GetType().GetProperties();
            var tr = "<tr>";

            foreach(var property in properties) {
                tr += String.Format("<td>{0}</td>", property.GetValue(_obj));
            }

            tr += "</tr>";

            return (tr);
        }

        public static string BuildTrHeader(object _obj)
        {
            var properties = _obj.GetType().GetProperties();
            var tr = "<tr>";

            foreach (var property in properties)
            {
                tr += String.Format("<th>{0}</th>", property.Name);
            }

            tr += "</tr>";

            return (tr);
        }

        public static HtmlString BuildTable(this HtmlHelper helper, object _obj)
        {
            if(!IsCollection(_obj.GetType())) {
                throw new InvalidOperationException("BuildTable helper can be called only on collection object");
            }

            var tableStart = String.Format(@"<table>
                            <thead>
                                {0}
                            </thead>
                            <tbody>", BuildTrHeader((_obj as IEnumerable<object>).ElementAt(0)));

            var tableBody = String.Empty;

            var coll = _obj as IEnumerable<object>;

            foreach(var _ in coll){
                tableBody += BuildTr(_);
            }

            var tableEnd = @"
                            </tbody>
                        </table>";;

            return new HtmlString(tableStart + tableBody + tableEnd);
        }

        static bool IsCollection(Type type)
        {
            return type.GetInterface(typeof(IEnumerable<>).FullName) != null;
        } 
    }
}

示例 View :

@{
    Layout = null;

    var objs = new List<object>();

    objs.Add(new { a = "Hello", b = "World" });
    objs.Add(new { a = "World", b = "Hello" });
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Test</title>
</head>
<body>
    @Html.BuildTable(objs)
</body>
</html>

关于c# - 如何使用动态模型创建动态表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29676230/

相关文章:

C# - IComparer - 如果 datetime 为空,则应排序到底部而不是顶部

ruby-on-rails - Rails for Zombies - 实验室

php - Html onclick 似乎在 echo PhP 中不起作用

asp.net - 使用 SSL 在服务器上部署 ASP.Net MVC 5 应用程序

javascript - 如何操作JSON数据?

c# - 当我的应用程序崩溃时,如何执行特定的代码块?

c# - 在 ASP.NET Core 1.0 中使用 ModelState 和 RedirectToAction 进行异步

c# - C# 语言命令的 Razor 语法

javascript - 速度滚动动画 jQuery

c# - 我应该在 ASP.NET MVC 中的哪里存储我的临时 View 模型?