我想创建一个表,所以我可以在其中加载一个类,并自动生成表。
这就是我要找的,但我不知道我怎么能意识到这一点:
@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>
}
这是我的ViewModel:
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,但这不起作用。这是某种方式吗?或者我是否必须使用单独的表格制作单独的视图?
答案 0 :(得分:2)
您需要针对此方案进行反思。你应该建立一个新的Html Helper。这个帮助器遍历模型的所有公共属性,并创建一个包含该信息的表,然后您只需要在视图中调用helper:
助手班级:
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;
}
}
}
示例视图:
@{
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>
答案 1 :(得分:0)
看看ModelMetadata。 另请参阅ModelMetadataProvider,甚至可以查看CustomMetadataProvider。
我使用它们来一般地渲染完整的编辑器对话框。
ModelMetadata metadata = (ModelMetadata)ViewData.ModelMetadata;
ModelMetadata[] properties = metadata.Properties.Where(pm => pm.ShowForEdit && (pm.IsReadOnly || !ViewData.TemplateInfo.Visited(pm))).ToArray();
foreach (TemplateMetadata prop in properties)
{
if (!prop.HideSurroundingHtml)
{
@Html.Label(prop.PropertyName, prop.DisplayName + ((prop.ModelType != typeof(bool) && prop.IsRequired) ? " *" : ""))
}
}