C# C反射使用变量作为对象。[var]
我试图在razor视图中生成一个表,使用反射从模型中提取属性 以下是我尝试过的:C# C反射使用变量作为对象。[var],c#,asp.net-mvc-4,razor,system.reflection,C#,Asp.net Mvc 4,Razor,System.reflection,我试图在razor视图中生成一个表,使用反射从模型中提取属性 以下是我尝试过的: @if (@Model.Count() > 0) { System.Reflection.PropertyInfo[] properties = Model.First().GetType().GetProperties(); <table> <thead> <tr> @foreach (va
@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>
}
其中,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; }
}
}
如果您真的不需要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>
如果您真的不需要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>
所以基本上我需要根据存储在变量中的属性的名称来访问item属性的值
否,您需要基于描述属性的PropertyInfo对象访问属性的值。那要容易得多
property.GetValue(item)
所以基本上我需要根据存储在变量中的属性的名称来访问item属性的值
否,您需要基于描述属性的PropertyInfo对象访问属性的值。那要容易得多
property.GetValue(item)
我可以建议使用视图模型吗?@DanielA.White使用视图模型对我有什么好处?我真的很喜欢用最少的手写代码来实现这种方法。关注点分离。您的代码失去了可维护性。反射是编写代码最低效的方法。少用,不要经常使用!我不能理解关注点的分离,我觉得这种方法完全将视图与数据分离。松散耦合很好,所以如果我更改数据,我就不必更改视图。我可以建议使用视图模型吗?@DanielA.White使用视图模型对我有什么好处?我真的很喜欢用最少的手写代码来实现这种方法。关注点分离。您的代码失去了可维护性。反射是编写代码最低效的方法。少用,不要经常使用!我不能理解关注点的分离,我觉得这种方法完全将视图与数据分离。松耦合很好,所以如果我更改数据,我不必更改视图。在表中,这只是打印propertyName,我试图使用DisplayFor获取propertyValue。如何访问底层数据?@SimonBelanger感谢在表中,这只是打印propertyName,我试图使用DisplayFor获取propertyValue。如何访问底层数据?@SimonBelanger Thanksusing property.GetValueitem在我的数据表中生成以下位置:PCNWeb.Models.Switch\u。是否有办法获取实际数据?这应该会给出实际数据。@TechplexEngineer:我认为您的Html.DisplayFor正在调用对象上的ToString。如果要更改字符串,请编写一个更好的开关\u Location.toStringOhh,因此我在上面添加了我的开关模型,因为开关有子对象,我需要一种方法来获取它们的值。@BenVoigt DisplayFor比这更复杂。它还检查成员上的属性,作为如何显示属性的提示。使用property.GetValueitem会在my datatable中生成以下位置:PCNWeb.Models.Switch\u。是否有办法获取实际数据?这应该会给出实际数据。@TechplexEngineer:我认为您的Html.DisplayFor正在调用对象上的ToString。如果要更改字符串,请编写一个更好的开关\u Location.toStringOhh,因此我在上面添加了我的开关模型,因为开关有子对象,我需要一种方法来获取它们的值。@BenVoigt DisplayFor比这更复杂。它还检查成员上的属性,作为如何显示属性的提示。