2

我正在尝试使用反射在剃刀视图中生成一个表,以从模型中提取属性。

这是我尝试过的:

@if (@Model.Count() > 0)
{
    System.Reflection.PropertyInfo[] properties = Model.First().GetType().GetProperties();
    <table>
        <thead>
            <tr>

            @foreach (var property in properties)
            {
                if (char.IsLower(property.Name.ToCharArray()[0])) //ignore foreign keys
                {
                    continue;
                }
                <th>@property.Name</th>
            }
            </tr>
        </thead>
        <tbody>
            @foreach (PCNWeb.Models.Switch item in Model)
            {
                /*System.Reflection.PropertyInfo[]*/ properties = item.GetType().GetProperties();
                <tr>
                @foreach (var property in properties)
                {                    
                    <td>
                        @Html.DisplayFor(modelItem => item.[property.Name])
                    </td>
                }
                </tr>

            }
        </tbody>
    </table>
}

让我指出我不确定如何处理的代码部分:

<td>
    @Html.DisplayFor(modelItem => item.[property.Name])
</td>

property.Name包含我要访问的项目属性的名称。如果我要手写最里面的td一个例子是:

<td>
    @Html.DisplayFor(modelItem => item.Switch_Location)
</td>

"Switch_Location"的价值在哪里property.Name

所以基本上我需要根据存储在变量中的属性名称来访问 item 属性的值。


编辑添加模型:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace PCNWeb.Models
{
    public partial class Switch
    {
        public Switch()
        {
            this.Ports = new List<Port>();
            this.Switch_Location = new Switch_Location();
            this.Switch_Model = new Switch_Model();
            this.UPS = new UPS();
        }
        [Key]
        public int switchRecId { get; set; }
        [Required]
        public int locationRecId { get; set; }
        [Required]
        public int modelRecId { get; set; }
        //public int gatewayRecId { get; set; }
        [Required]
        public int upsRecId { get; set; }
        [Required]
        public int Number { get; set; }
        [Required]
        [StringLength(64)]
        public string Name { get; set; }
        [StringLength(80)]
        public string Description { get; set; }

        [StringLength(32)]
        public string Cabinet { get; set; }

        [StringLength(40)]
        public string Power_Feed { get; set; }
        [Required]
        public Nullable<int> ipOctet1 { get; set; }
        [Required]
        public Nullable<int> ipOctet2 { get; set; }
        [Required]
        public Nullable<int> ipOctet3 { get; set; }
        [Required]
        public Nullable<int> ipOctet4 { get; set; }

        public virtual ICollection<Port> Ports { get; set; }
        public virtual Switch_Location Switch_Location { get; set; }
        public virtual Switch_Model Switch_Model { get; set; }
        public virtual UPS UPS { get; set; }
    }
}
4

2 回答 2

4

所以基本上我需要根据存储在变量中的属性名称来访问 item 属性的值。

不,您需要根据PropertyInfo描述它的对象访问属性的值。这要容易得多。

property.GetValue(item)
于 2013-07-17T14:26:48.070 回答
0

如果你真的不需要 DisplayFor 方法,你可以在你的循环中这样做:

<tbody>
            @foreach (PCNWeb.Models.Switch item in Model)
            {
                /*System.Reflection.PropertyInfo[]*/ properties = item.GetType().GetProperties();
                <tr>
                @foreach (var property in properties)
                {                    
                    <td>
                        @property.GetValue(item,null)
                    </td>
                }
                </tr>

            }
        </tbody>
于 2013-07-17T14:25:38.693 回答