"Enter"a basıp içeriğe geçin

Asp .Net MVC – Dinamik Tablo Oluşturma

@model IEnumerable<object>

@{
    string[] includTypes = new string[]
    {
        "System.Boolean",
        "System.Byte",
        "System.SByte",
        "System.Char",
        "System.Decimal",
        "System.Double",
        "System.Single",
        "System.Int32",
        "System.UInt32",
        "System.Int64",
        "System.UInt64",
        "System.Int16",
        "System.UInt16",
        "System.String"
    };
    var first = Model.FirstOrDefault();
    if (first != null)
    {
        var firstProps = first.GetType().GetProperties();
        string tableId = "table" + first.GetType().Name;

        <table id="@tableId"
               data-show-columns="true"
               no-records-found="false"
               data-pagination="true"
               data-search="true">
            <thead>
                <tr>
                    @foreach (var pInfo in firstProps)
                    {
                        if (pInfo != null && includTypes.Any(m => m == pInfo.PropertyType.FullName))
                        {
                            var translate = ContextResource.ResourceManager.GetString(pInfo.Name);
                            translate = string.IsNullOrWhiteSpace(translate) ? pInfo.Name : translate;
                            var visible = pInfo.Name.EndsWith("Id") ? "false" : "true";
                            <th data-field="@pInfo.Name" data-visible="@visible"> @translate</th>

                        }
                    }
                </tr>
            </thead>
            <tbody>
                @foreach (var item in Model)
                {
                    var props = item.GetType().GetProperties();

                    <tr>
                        @foreach (var pInfo in props)
                        {
                            if (pInfo != null && includTypes.Any(m => m == pInfo.PropertyType.FullName))
                            {
                                var value = pInfo.GetValue(item, null);
                                if (value != null)
                                {
                                    <td>@value.ToString()</td>
                                }
                                else
                                {
                                    <td>-</td>
                                }

                            }
                        }
                    </tr>
                }
            </tbody>
        </table>
     }
}

 

Önerilen table scripti : http://bootstrap-table.wenzhixin.net.cn/

İlk Yorumu Siz Yapın

Bir Cevap Yazın

E-posta hesabınız yayımlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir