我现在有以下剃刀线:
<table border=1 cellpadding=3 cellspacing=1 rules="rows" frame="box">
<tr>
<th>Türkçe Söz Dizisi</th>
<th>English Word Sequence</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Tr)
</td>
<td>
@Html.DisplayFor(modelItem => item.En)
</td>
<td>
@Html.ActionLink((String)@ViewBag.Edit, "Edit", new { id=item.ID }) |
@Html.ActionLink((String)@ViewBag.Delete, "Delete", new { id=item.ID })
</td>
</tr>
}
</table>
但是,我会自动将一些列从程序添加到数据库表中。我需要一种方法来相应地打印这个 th 和 td 。简而言之,我需要一种方法来浏览模型的动态列。我怎样才能实现这个目标?
EDIT:我的模型类型是“提案”。但是,我想达到 Proposal.Type.Word(Tr、En 或任何其他添加的语言枚举)的动态属性。我怎么能够?
@foreach (var item in Model) {
Type objectType = Type.GetType(typeof(RexamOneriSistemi.Models.Word).AssemblyQualifiedName);
System.Reflection.PropertyInfo[] fields = objectType.GetProperties();
<tr>
<td>@Html.DisplayFor(modelItem => item.ID)</td>
<td>@Html.DisplayFor(modelItem => item.Owner.Name)</td>
@foreach (System.Reflection.PropertyInfo f in fields) {
if (f.Name.ToString() == @HttpContext.Current.Session["Language"].ToString())
{
<td>
// What to do here exactly to get item.Type.Word.[dynamic attribute]?
</td>
}
</tr>
}
我可以获得剃刀绳
string s = "@Html.Displayfor(modelItem => item.Type.Word." + System.Web.HttpContext.Current.Session["Language"] + ")"
Resulting: @Html.Displayfor(modelItem => item.Type.Word.Tr)
我可以插入要呈现为 Razor 语法的字符串吗?如果是,怎么办?
我已经尝试过这段代码并且它有效
<table border=1 cellpadding=3 cellspacing=1 rules="rows" frame="box">
<tr>
@{
Type objectType = Type.GetType(typeof(yourProjectNamespace.Models.Language).AssemblyQualifiedName);
System.Reflection.PropertyInfo[] fields = objectType.GetProperties();
foreach (System.Reflection.PropertyInfo f in fields)
{
<th> @f.Name.ToString() </th>
}
}
</tr>
@foreach (yourModelType item in Model) {
<tr>
foreach (System.Reflection.PropertyInfo f in fields)
{
<td>@Html.Display(f.Name.ToString())</td>
}
<td>
@Html.ActionLink((String)@ViewBag.Edit, "Edit", new { id=item.ID }) |
@Html.ActionLink((String)@ViewBag.Delete, "Delete", new { id=item.ID })
</td>
</tr>
}
</table>
要获取您的类的程序集限定名称,请参阅此链接here https://stackoverflow.com/questions/3758209/why-would-system-type-gettypexyz-return-null-if-typeofxyz-exists
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)